美文网首页
Exception - java.lang.IllegalSta

Exception - java.lang.IllegalSta

作者: 许先森的许 | 来源:发表于2016-06-30 11:09 被阅读70次

    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

    使用fragment的时候出现这个exception:

    java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManagerImpl.java:1377)

    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManagerImpl.java:1395)

    是在使用FragmentTransaction,commit方法添加一个fragment出现的错误。

    官网说明:http://developer.android.com/reference/android/app/FragmentTransaction.html#commitAllowingStateLoss()

    public abstract intcommitAllowingStateLoss()

    Added inAPI level 11

    Likecommit()but allows the commit to be executed after an activity's state is saved. This is dangerous because the commit can be lost if the activity needs to later be restored from its state, so this should only be used for cases where it is okay for the UI state to change unexpectedly on the user.

    意思是说commit方法不可以在onSaveInstanceState 之后调用,因为onSaveInstanceState方法是activity在即将被销毁前调用,来保存activity状态,如果在保存完后再给它添加fragment就会出错。

    解决方法:把commit方法换成commitAllowingStateLoss()即可,效果一样。

    相关文章

      网友评论

          本文标题:Exception - java.lang.IllegalSta

          本文链接:https://www.haomeiwen.com/subject/uroxjttx.html