java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
来源:互联网 发布:java反射机制的优缺点 编辑:程序博客网 时间:2024/05/17 00:59
公司app线上这个报错量一直很高,这次修复了下,分析下整个原因:
整个报错栈信息如下:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState at android.support.v4.app.FragmentManagerImpl.checkStateLoss(SourceFile:1527) at android.support.v4.app.FragmentManagerImpl.enqueueAction(SourceFile:1545) at android.support.v4.app.BackStackRecord.commitInternal(SourceFile:654) at android.support.v4.app.BackStackRecord.commit(SourceFile:621) at android.support.v4.app.FragmentTabHost.onAttachedToWindow(SourceFile:282) at android.view.View.dispatchAttachedToWindow(View.java:13326) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2601) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2608) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1334) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1096) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6057) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:793) at android.view.Choreographer.doCallbacks(Choreographer.java:606) at android.view.Choreographer.doFrame(Choreographer.java:575) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:779) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5539) 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)
1、栈顶原因分析:
FragmentManager.checkStateLoss这里抛的异常,翻下源码会发现:如果FragmentTransaction.commit时候activity的状态被保存(即调用了onsaveInstance回调后),就会在这个地方抛这样的异常;
2、相关知识点:
a、在android HONEYCOMB(Api11)之前:onsaveinstance回调是在onpause之前,在Api11之后调整到了opause之后onstop之前;
b、从报错信息栈里可以看出我这个项目里用了:FragmentTabHost控件,FragmentTabHost在调用addTab时候会调事务的commit;
c、FragmentTransaction.commit接口是不允许在activity的状态保存之后调用(即调用了onsaveInstance回调后)
3、解决方案:
a、由于v4支持包中FragmentActivity的onsaveInstance回调存在bug,在回调这个方法时候有可能会有异常,这里重写这个方法不去调用super:
@Override protected void onSaveInstanceState(Bundle outState) {// super.onSaveInstanceState(outState); //No call for super(). Bug on API Level > 11. }
b、使用FragmentTransaction.commitAllowingStateLoss来提交事务,但由于我这里提交事务是在源码里的,无法复写,故没法处理了;
c、通过复写FragmentTabHost的onAttachedToWindow方法捕获异常:
@Override protected void onAttachedToWindow() { try { super.onAttachedToWindow(); } catch (IllegalStateException e) { //由于ft.commit(); } }
后续再看有没有什么好方法。
1 0
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException:Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState问题解决
- Fragment解决java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- Fragment java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- 快速幂取模(当数很大时,相乘long long也会超出的解决办法)
- cmake编译opencv3.1+contribute模块,并提供编译后的dll、lib等文件下载
- HDU 1242 BFS-Rescue
- tabLeview的静态分组使用
- 封锁机制以及读锁和写锁的区别
- java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
- 简单用tcp实现一个client和sever间通信
- 男人八题其二(POJ 1738) 石子合并 GarsiaWachs算法
- nyoj 244 16 进制简单运算
- Spring Data JPA
- 【HDU】2196 - Computer(树的直径)
- c++ primer 学习(四) 函数
- C#控制台基础 结构,类在声明的同时初始化
- 关于JavaScript substr() 方法