Android RuntimeException onCreateDialog没有为id创建对话框

zeg*_*nus 6 android runtimeexception

我有一个应用程序,您可以显示和关闭几个对话框:

showDialog(...)
removeDialog(...)
Run Code Online (Sandbox Code Playgroud)

我在应用程序中玩了一下,当屏幕上没有任何Dialog时,我按下菜单按钮,然后进入主安卓屏幕.

过了一会儿,我再次进入我的应用程序,有时,我得到这个RuntimeException:

java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 4
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2621)
    at android.app.ActivityThread.access$2200(ActivityThread.java:126)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1932)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4595)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 4
    at android.app.Activity.createDialog(Activity.java:878)
    at android.app.Activity.restoreManagedDialogs(Activity.java:867)
    at android.app.Activity.performRestoreInstanceState(Activity.java:815)
    at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2565)
    ... 11 more
Run Code Online (Sandbox Code Playgroud)

任何的想法?

非常感谢你.

更新,更多信息:

当前的onCreateDialog实现是:

protected Dialog onCreateDialog(int id){
 Builder b = new AlertDialog.Builder(this);
 if(id == 4){
  b.setMessage(...);
  b.setItems(items, new DialogInterface.OnClickListener(){
   public void onClick(DialogInterface dialog, int which){
    Intent i = new Intent(Current.this, Another.class);
    startActivity(i);
   }
  });
  return b.create();
 }
 return null;
}
Run Code Online (Sandbox Code Playgroud)

为了调用这个函数我做:

removeDialog(4);
showDialog(4);
Run Code Online (Sandbox Code Playgroud)

mnd*_*rix 11

在API级别8,onCreateDialog(int)弃用的青睐onCreateDialog(int,Bundle).如果仅实现后一种方法并在API级别低于8的设备上运行应用程序,则会收到所描述的错误消息.

解决方案是实施 onCreateDialog(int)


小智 2

removeDialog在经历了同样的问题(并发现从内部调用onPause不能可靠地工作)之后,我开发了一种似乎有效的解决方法(尽管这无疑是一种黑客行为)。

正如antslava 发布的 grepcode 链接中所见,在 method 中performRestoreInstanceStateonRestoreInstanceState在之前调用restoreManagedDialogs并传递了相同的Bundle savedInstanceState.

final void performRestoreInstanceState(Bundle savedInstanceState) {
    onRestoreInstanceState(savedInstanceState);
    restoreManagedDialogs(savedInstanceState);
}
Run Code Online (Sandbox Code Playgroud)

因此,有机会修改从方法内Bundle savedInstanceState传递到的。restoreManagedDialogsonRestoreInstanceState

为了防止恢复任何和所有托管对话框,可以onRestoreInstanceState通过以下方式实现:

// This same variable is defined as private in the Activity class. I need
// access to it, so I redefine it here.
private static final String SAVED_DIALOGS_TAG = "android:savedDialogs";

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
    if (null != b) {
        savedInstanceState.remove(SAVED_DIALOGS_TAG);
    }
}
Run Code Online (Sandbox Code Playgroud)

这会导致Bundle将 key 引用的键"android:savedDialogs"从 中删除,随后当发现无法找到该键时,Bundle savedInstanceState会导致对 的调用立即返回:restoreManagedDialogs

private void restoreManagedDialogs(Bundle savedInstanceState) {
    final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
    if (b == null) {
        return;
    }
    ...
}
Run Code Online (Sandbox Code Playgroud)

这将导致在恢复活动时不被调用,从而有效地“隐藏”任何对话框,从而防止发生必须返回onCreateDialog的情况。nullonCreateDialog

这不是“一刀切”的解决方案,但考虑到我的要求,它似乎符合要求。通过检查多个平台版本(1.6、2.1、2.2、2.2.2 和 4.0.3)的 grepcode 中的代码,看来该解决方案应该在这些现有实现的情况下一致地工作。