2011-01-28 42 views
8

這個讓我陷入瘋狂的邊緣!「在添加內容之前必須調用android.util.AndroidRuntimeException:requestFeature()on showDialog(dialogId)

我有,我初始化按鈕查看與onClickListener,像這樣的活動:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.myLayout); 

    mMyButton = (Button) findViewById(R.id.myButtonId); 
    mMyButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      showDialog(ID_MYDIALOG); 
     } 
    }); 
} 

我也寫我自己的對話框生成器類(單身),因爲我有其他幾個活動在我的應用程序這將在使用一組共同的對話框,從而在onCreateDialog(int id)函數對於給定的活動我稱:

@Override 
protected Dialog onCreateDialog(int id) { 
    Dialog dialog; 

    switch (id) { 
     case ID_MYDIALOG: 
      dialog = DialogBuilder.getInstance().getMyDialog(this, mUri, mListener); 
      break; 

     default: 
      dialog = null; 
      break; 
    } 

    return dialog; 
} 

mUrimListener變量別處定義和我已驗證第ey是有效的。單身DialogBuilder類的相關部分如下:

public AlertDialog getMyDialog(Context context, Uri uri, DialogInterface.OnClickListener listener) { 
    // Inflate the custom body for the AlertDialog. 
    LayoutInflater layoutInflater = LayoutInflater.from(context); 
    View view = layoutInflater.inflate(R.layout.myDialogBody, null); 

    // Get the data to show in the dialog. 
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 

    // Create a suitable data adapter and use it to populate the dialog body with data. 
    MyDialogDataAdapter adapter = new MyDialogDataAdapter(context, cursor); 

    // Populate the GridView in the dialog body with data. 
    GridView grid = (GridView) view.findViewById(R.id.myDialogDataGrid); 
    grid.setAdapter(adapter); 

    return new AlertDialog.Builder(context) 
    .setIcon(R.drawable.myDialogIcon) 
    .setTitle(R.string.myDialogTitle) 
    .setView(view) 
    .setPositiveButton(R.string.myDialogDone, listener) 
    .create(); 
} 

現在,當我跑我的應用程序,然後點擊按鈕(即mMyButton)我得到以下異常堆棧跟蹤:

E/AndroidRuntime(1247): FATAL EXCEPTION: main 
E/AndroidRuntime(1247): android.util.AndroidRuntimeException: requestFeature() must be called before adding content 
E/AndroidRuntime(1247): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181) 
E/AndroidRuntime(1247): at com.android.internal.app.AlertController.installContent(AlertController.java:203) 
E/AndroidRuntime(1247): at android.app.AlertDialog.onCreate(AlertDialog.java:251) 
E/AndroidRuntime(1247): at android.app.Dialog.dispatchOnCreate(Dialog.java:307) 
E/AndroidRuntime(1247): at android.app.Activity.createDialog(Activity.java:886) 
E/AndroidRuntime(1247): at android.app.Activity.showDialog(Activity.java:2557) 
E/AndroidRuntime(1247): at android.app.Activity.showDialog(Activity.java:2524) 
E/AndroidRuntime(1247): at com.dbm.myApp.myActivity$4.onClick(myActivity.java:266) 
E/AndroidRuntime(1247): at android.view.View.performClick(View.java:2534) 
E/AndroidRuntime(1247): at android.view.View$PerformClick.run(View.java:9210) 
E/AndroidRuntime(1247): at android.os.Handler.handleCallback(Handler.java:587) 
E/AndroidRuntime(1247): at android.os.Handler.dispatchMessage(Handler.java:92) 
E/AndroidRuntime(1247): at android.os.Looper.loop(Looper.java:123) 
E/AndroidRuntime(1247): at android.app.ActivityThread.main(ActivityThread.java:3652) 
E/AndroidRuntime(1247): at java.lang.reflect.Method.invokeNative(Native Method) 
E/AndroidRuntime(1247): at java.lang.reflect.Method.invoke(Method.java:507) 
E/AndroidRuntime(1247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
E/AndroidRuntime(1247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
E/AndroidRuntime(1247): at dalvik.system.NativeStart.main(Native Method) 
W/ActivityManager( 212): Force finishing activity com.dbm.myApp/.myActivity 

如果我正確解釋堆棧跟蹤問題是在我點擊我的按鈕(從com.dbm.myApp.myActivity$4.onClick(myActivity.java:266)這是確切的showDialog(ID_MYDIALOG);行)的那一刻觸發的。

你是什麼人,同行的Android開發人員說的?你認爲是什麼導致我的問題?

根據編譯和單元測試等,假設上面的代碼是有效的,然而,給出的代碼被簡化並且被剝離以適應論壇,因此請不要努力發表評論命名約定等。

+0

我現在已經測試過在singleton DialogBu​​ilder類的`getMyDialog`函數中註釋掉所有內容,同樣的問題仍然存在。我開始認爲我的Activity沒有正確初始化。 – dbm 2011-01-28 12:52:40

+0

我遇到過這個,但我不記得確切的修復:-(你需要調用:`requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);`在你的DialogBu​​ilder類中? – Blundell 2011-01-28 09:11:27

+0

一個很好的提示,我只是測試它,但不幸的是問題依然存在。此外,我可能需要在requestFeature()函數(或簡寫版本:requestWindowFeature())上讀一些內容,以及它們對我的應用程序的真實作用。 – dbm 2011-01-28 09:22:48

回答

17

我想我可能已經解決了這個問題:如果我出於某種原因創建AlertDialog有:

return new AlertDialog.Builder(context) 
    .setIcon(R.drawable.myDialogIcon) 
    .setTitle(R.string.myDialogTitle) 
    .setView(view) 
    .setPositiveButton(R.string.myDialogDone, listener) 
    .show(); 

即與.show();而不是.create();問題不再存在。我不知道爲什麼這會起作用,或者函數與AlertDialog.Builder::show()函數不同(不同之處在於各個函數名稱中反映的明顯差異)。

3

嘗試在構造函數後面調用.setView(view)

return new AlertDialog.Builder(context) 
    .setView(view) 
    .setIcon(R.drawable.myDialogIcon) 
    .setTitle(R.string.myDialogTitle) 
    .setPositiveButton(R.string.myDialogDone, listener) 
    .create(); 

它有幫助嗎?

+0

我現在也測試了你的建議。我也嘗試先創建我的對話框,並在實例化之後爲它賦值,但是失敗了。 – dbm 2011-01-28 10:04:26

1

它可能不會,我有點被這片驚訝的solution..but:

mMyButton.setOnClickListener(new View.OnClickListener() { 
    showDialog(ID_MYDIALOG); 

我想你應該實現的onClick在監聽器裏?我錯了嗎?

相關問題