致命异常:java.lang.IllegalStateException:在onSaveInstanceState之后无法执行此操作

时间:2016-06-29 09:33:30

标签: android illegalstateexception

我在按钮onClick事件中显示一个DialogFragment,如下所示:

public void  onButtonClick() {
    myDialogFragment.show(getSupportFragmentManager(), "dialog");
}

为什么它会导致IllegalStateException,可能在onSaveInstanceState之后调用? 这是日志:

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
   at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1377)
   at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1395)
   at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
   at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
   at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
   at com.a.b.AFragment.editSex(AFragment.java:226)
   at com.a.b.AFragment.access$200(AFragment.java:46)
   at com.a.b.AFragment$3.onButtonClick(AFragment.java:134)

1 个答案:

答案 0 :(得分:1)

在show方法中调用此

fragmentTransaction.commitAllowingStateLoss()
将片段添加到

fragmentTransaction.add();

供参考

  

与提交类似,但允许在保存活动状态后执行提交。这很危险,因为如果活动需要稍后从其状态恢复,则提交可能会丢失,因此这应仅用于UI状态可以在用户上意外更改的情况。