越来越例外" IllegalStateException异常:无法执行的onSaveInstanceState&QUOT后,这个动作; [英] getting exception "IllegalStateException: Can not perform this action after onSaveInstanceState"
问题描述
我有一个活的Android应用程序,并从市场上我已经收到以下堆栈跟踪,我不知道为什么它的发生因为它不是发生在应用程序code,但它越来越引起部分或其他事件的应用程序(假设)
我不使用碎片,仍然有FragmentManager的参考。 如果任何机构可以抛出一些隐藏的事实,一些光以避免这种类型的问题:
java.lang.IllegalStateException:以后的onSaveInstanceState无法执行此操作
在android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109)
在android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399)
在android.app.Activity.onBack pressed(Activity.java:2066)
在android.app.Activity.onKeyDown(Activity.java:1962)
在android.view.KeyEvent.dispatch(KeyEvent.java:2482)
在android.app.Activity.dispatchKeyEvent(Activity.java:2274)
在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1668)
在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchKeyEvent(PhoneWindow.java:1720)
在com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1258)
在android.app.Activity.dispatchKeyEvent(Activity.java:2269)
在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1668)
在android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2851)
在android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2824)
在android.view.ViewRoot.handleMessage(ViewRoot.java:2011)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:132)
在android.app.ActivityThread.main(ActivityThread.java:4025)
在java.lang.reflect.Method.invokeNative(本机方法)
在java.lang.reflect.Method.invoke(Method.java:491)
在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841)
在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
在dalvik.system.NativeStart.main(本机方法)
这是我迄今遇到的最愚蠢的错误。我有一个 片段
应用程序可以正常使用了 API< 11 和 强制关闭
在 API> 11
我实在想不出他们的活动内改变
生命周期中调用 saveInstance
,但我在这里是我如何解决这样的:
@覆盖
保护无效的onSaveInstanceState(包outState){
//没有要求超()。错误的API级别> 11。
}
我只是不使。超()调用,一切的伟大工程。我希望这将节省您的时间。
编辑:之后,一些调查研究,这是一个已知的中的支持包的bug 。
如果您需要保存的实例,并添加一些东西到你的 outState
捆绑
您可以使用以下命令:
@覆盖
保护无效的onSaveInstanceState(包outState){
outState.putString(WORKAROUND_FOR_BUG_19917_KEY,WORKAROUND_FOR_BUG_19917_VALUE);
super.onSaveInstanceState(outState);
}
EDIT2:的可能,如果你想你的活动
消失在后台后执行事务也会发生。为了避免这种情况,你应该使用 commitAllowingStateLoss()
EDIT3:将上述溶液固定从我记得在早期support.v4库的问题。但是,如果你仍然有问题,这个您必须也看过 @AlexLockwood 的博客:<一href="http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html">Fragment交易及放大器;活动状态损耗
从博客文章摘要(但我强烈建议你读它):
- 绝不会
提交()
在的onPause交易()
在pre -Honeycomb和的onStop()
在后期蜂窝 - 承诺在
活动
生命周期方法的交易时要小心。 使用的onCreate()
,onResumeFragments()
和onPostResume( )
- 避免内部异步回调方法执行交易
- 使用
commitAllowingStateLoss()
只能作为最后的手段
I have a Live Android application, and from market i have received following stack trace and i have no idea why its happening as its not happening in application code but its getting caused by some or the other event from the application (assumption)
I am not using Fragments, still there is a reference of FragmentManager. If any body can throw some light on some hidden facts to avoid this type of issue:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1109)
at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:399)
at android.app.Activity.onBackPressed(Activity.java:2066)
at android.app.Activity.onKeyDown(Activity.java:1962)
at android.view.KeyEvent.dispatch(KeyEvent.java:2482)
at android.app.Activity.dispatchKeyEvent(Activity.java:2274)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1668)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1112)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1720)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1258)
at android.app.Activity.dispatchKeyEvent(Activity.java:2269)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1668)
at android.view.ViewRoot.deliverKeyEventPostIme(ViewRoot.java:2851)
at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2824)
at android.view.ViewRoot.handleMessage(ViewRoot.java:2011)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4025)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
This is the most stupid bug I have encountered so far. I had a Fragment
application working perfectly for API < 11, and Force Closing
on API > 11.
I really couldn't figure out what they changed inside the Activity
lifecycle in the call to saveInstance
, but I here is how I solved this :
@Override
protected void onSaveInstanceState(Bundle outState) {
//No call for super(). Bug on API Level > 11.
}
I just do not make the call to .super()
and everything works great. I hope this will save you some time.
EDIT: after some more research, this is a known bug in the support package.
If you need to save the instance, and add something to your outState
Bundle
you can use the following :
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}
EDIT2: this may also occur if you are trying to perform a transaction after your Activity
is gone in background. To avoid this you should use commitAllowingStateLoss()
EDIT3: The above solutions were fixing issues in the early support.v4 libraries from what I can remember. But if you still have issues with this you MUST also read @AlexLockwood 's blog : Fragment Transactions & Activity State Loss
Summary from the blog post (but I strongly recommend you to read it) :
- NEVER
commit()
transactions afteronPause()
on pre-Honeycomb, andonStop()
on post-Honeycomb - Be careful when committing transactions inside
Activity
lifecycle methods. UseonCreate()
,onResumeFragments()
andonPostResume()
- Avoid performing transactions inside asynchronous callback methods
- Use
commitAllowingStateLoss()
only as a last resort
这篇关于越来越例外&QUOT; IllegalStateException异常:无法执行的onSaveInstanceState&QUOT后,这个动作;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!