首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

Android requestFeature 必須在 setContentView 前呼叫:錯誤原因與解決方案

     編輯:程式設計綜合問答

錯誤訊息 requestFeature() must be called before adding content 表示在呼叫 setContentView() 後才嘗試請求視窗功能(如 FEATURE_CUSTOM_TITLE, FEATURE_NO_TITLE 等)。Android 視窗功能必須在佈局填充前決定,否則拋出異常。

錯誤代碼分析


// 錯誤順序
setContentView(R.layout.main);           // 1. 先添加內容
UIHelper.setupTitleBar(this);            // 2. 再請求功能 → 崩潰

// UIHelper.setupTitleBar 內部
c.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); // 此處拋出異常

正確順序


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // 1. 請求視窗功能(在 setContentView 之前)
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    // 或 requestWindowFeature(Window.FEATURE_NO_TITLE);
    
    // 2. 設定佈局
    setContentView(R.layout.main);
    
    // 3. 套用自訂標題佈局(若有)
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}

輔助類重構建議


public class UIHelper {
    public static void setupCustomTitle(Activity activity, int titleLayoutResId) {
        // 呼叫端必須確保在 setContentView 前調用此方法
        boolean supported = activity.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        if (supported) {
            activity.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, titleLayoutResId);
        }
    }
}

// 正確使用
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    UIHelper.setupCustomTitle(this, R.layout.titlebar); // 在 setContentView 前
    setContentView(R.layout.main);
}

替代方案:DialogFragment / AlertDialog 自訂標題

若僅需對話框自訂標題,建議改用 DialogFragment 或 AlertDialog.Builder.setCustomTitle(View),避免 Activity 級視窗功能限制:


AlertDialog dialog = new AlertDialog.Builder(this)
    .setCustomTitle(LayoutInflater.from(this).inflate(R.layout.custom_title, null))
    .setView(contentView)
    .create();
dialog.show();

核心原則總結

視窗功能請求 → 佈局填充 → 功能配置
任何 requestWindowFeature()、setFeatureInt() 必須在 setContentView() 之前完成。

常見衍生錯誤

  • 在 onPostCreate()、onStart() 中請求功能 → 太晚,會崩潰。
  • 多次呼叫 setContentView() 導致重複添加內容。
  • 主題已設定 NoActionBar 再請求 FEATURE_CUSTOM_TITLE 衝突。

嚴守呼叫順序,即可避免此經典異常。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved