Android RuntimeException onCreateDialog did not create a dialog for id

前端 未结 5 953
温柔的废话
温柔的废话 2021-02-09 19:33

I\'ve an application that you can show and close several Dialogs with:

showDialog(...)
removeDialog(...)

I play a little bit with the applicati

相关标签:
5条回答
  • 2021-02-09 19:56

    Are you properly returning the dialog in onCreateDialog? If you were to do dialog.show() in dialog create but return some other dialog you could perhaps get a result like that.

    Or are you doing any sort of manipulation in of the dialog object in onPrepareDialog

    0 讨论(0)
  • 2021-02-09 20:02

    Have you implemented OnCreateDialog as presented here? When you call showDialog(4) for the first time, OnCreateDialog(4) will be called and you need to create the dialog and return it from this method.

    0 讨论(0)
  • 2021-02-09 20:10

    In API level 8, onCreateDialog(int) was deprecated in favor of onCreateDialog(int,Bundle). If you implement only the latter method and run the app on a device with an API level lower than 8, you get the described error message.

    The solution is to implement onCreateDialog(int)

    0 讨论(0)
  • 2021-02-09 20:11

    For SDK version < 8, if you return null in onCreateDialog you get Exception java.lang.IllegalArgumentException.

    0 讨论(0)
  • 2021-02-09 20:12

    After experiencing this same issue (and finding that calling removeDialog from within onPause doesn't work reliably), I developed a workaround that seems to function (although it's admittedly a hack).

    As seen in the grepcode link posted by antslava, in method performRestoreInstanceState, onRestoreInstanceState is called right before restoreManagedDialogs and is passed the same instance of Bundle savedInstanceState.

    final void performRestoreInstanceState(Bundle savedInstanceState) {
        onRestoreInstanceState(savedInstanceState);
        restoreManagedDialogs(savedInstanceState);
    }
    

    Thus, there is opportunity to modify the Bundle savedInstanceState that is passed to restoreManagedDialogs from within the onRestoreInstanceState method.

    To prevent any and all managed dialogs from being restored, one could implement onRestoreInstanceState in the following way:

    // 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);
        }
    }
    

    This causes the Bundle referenced by key "android:savedDialogs" to be removed from Bundle savedInstanceState, which subsequently causes the call to restoreManagedDialogs to immediately return when it finds that this key cannot be found:

    private void restoreManagedDialogs(Bundle savedInstanceState) {
        final Bundle b = savedInstanceState.getBundle(SAVED_DIALOGS_TAG);
        if (b == null) {
            return;
        }
        ...
    }
    

    This will cause onCreateDialog to not be called while restoring the Activity, effectively "hiding" any dialogs, thus preventing the scenario where one must return null from onCreateDialog from occurring.

    This isn't a 'one size fits all' solution, but given my requirements it seems to fit the bill. By reviewing the code in grepcode for several platform versions (1.6, 2.1, 2.2, 2.2.2, and 4.0.3), it appears that this solution should work consistently given these existing implementations.

    0 讨论(0)
提交回复
热议问题