解决IllegalStateException: Can not perform this action after onSaveInstanceState
錯誤信息:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1489)at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1507)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
at com.zte.iptvclient.android.iptvclient.activity.MainFragmentActivity.a(MainFragmentActivity.java:2137)
at com.zte.iptvclient.android.iptvclient.activity.MainFragmentActivity.a(MainFragmentActivity.java:2297)
at com.zte.iptvclient.android.iptvclient.activity.MainFragmentActivity.a(MainFragmentActivity.java:2230)
at com.zte.iptvclient.android.iptvclient.activity.MainFragmentActivity.a(MainFragmentActivity.java:2937)
at com.zte.iptvclient.android.baseclient.f.f.j.q(LoginImplement.java:1845)
at com.zte.iptvclient.android.baseclient.f.f.j.c(LoginImplement.java:75)
at com.zte.iptvclient.android.baseclient.f.f.k.a(LoginImplement.java:308)
at com.zte.iptvclient.android.androidsdk.uiframe.j.b(CommonListDataLoader.java:10355)
at com.zte.iptvclient.android.androidsdk.uiframe.k.handleMessage(CommonListDataLoader.java:206)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5669)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
代碼:
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
if (fragment.isAdded())
{
Log.w(LOG_TAG, fragment.getFragmentTitle() + "fragment is added.");
//return;
fragmentTransaction.remove(fragment);
}
setTitle(fragment.getFragmentTitle());
fragmentTransaction.replace(R.id.main_fragment_container, fragment, tag);
fragmentTransaction.commit(); ?--- ?這一行報錯
不合法原因:
Like?commit()?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方法是在Activity的onSaveInstanceState()之后調用的,這樣會出錯,因為onSaveInstanceState
? ?方法是在該Activity即將被銷毀前調用,來保存Activity數據的,如果在保存玩狀態后再給它添加Fragment就會出錯。
解決方法:
將這個方法 ?fragmentTransaction.commit(); 替換public abstract int?commitAllowingStateLoss?()? ? ? ? ? ?其效果是一樣的。
總結
以上是生活随笔為你收集整理的解决IllegalStateException: Can not perform this action after onSaveInstanceState的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机网络——DHCP协议详解(大学如何
- 下一篇: PHP如何操作json?方法介绍