讨论java.lang.IllegalStateException:onSaveInstanceState

时间:2017-01-24 07:51:07

标签: android illegalstateexception

我在屏幕旋转后调用下面的代码

getFragmentManager().beginTransaction().replace(R.id.fragment_placeholder, mPdfFragment).commit();

但是我收到以下错误消息:

FATAL EXCEPTION: main
Process: com.microsoft.pdfviewertestapplication, PID: 18799
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
    at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1415)
    at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1433)
    at android.app.BackStackRecord.commitInternal(BackStackRecord.java:687)
    at android.app.BackStackRecord.commit(BackStackRecord.java:663)
    at com.microsoft.pdfviewertestapplication.MainActivity.documentOpenedSuccessfully(MainActivity.java:263)
    at com.microsoft.pdfviewertestapplication.MainActivity.onDocumentOpened(MainActivity.java:548)
    at com.microsoft.pdfviewer.PdfFragment.onPdfFragmentPasswordDialogDocumentOpened(PdfFragment.java:2271)
    at com.microsoft.pdfviewer.PdfFragmentPasswordDialog.verifyAfterEnterPress(PdfFragmentPasswordDialog.java:311)
    at com.microsoft.pdfviewer.PdfFragmentPasswordDialog.access$600(PdfFragmentPasswordDialog.java:38)
    at com.microsoft.pdfviewer.PdfFragmentPasswordDialog$5.onEditorAction(PdfFragmentPasswordDialog.java:339)

我查看了FragmentManager.java的源代码:

if (mStateSaved) {
    throw new IllegalStateException(
       "Can not perform this action after onSaveInstanceState");
}

我发现布尔值mStateSaved很重要。我发现在onSaveInstanceState函数中它被设置为true。但是如果我带来片段背景和前进,它也会调用mStateSaved,所以我很困惑。如何从我的代码中设置布尔值?

PS:我需要在onResume之后调用commit。如果我把我的片段背景和前进,它会运行良好,它也会在onPause和onSaveInstance之后调用commit,所以我很困惑。

0 个答案:

没有答案