获取异常“IllegalStateException:onSaveInstanceState 后无法执行此操作"; [英] getting exception "IllegalStateException: Can not perform this action after onSaveInstanceState"

查看:50
本文介绍了获取异常“IllegalStateException:onSaveInstanceState 后无法执行此操作";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Live Android 应用程序,并且从市场上我收到了以下堆栈跟踪,但我不知道为什么它会发生,因为它没有发生在应用程序代码中,而是由应用程序中的某些或其他事件引起的(假设)

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)

我没有使用 Fragment,仍然有 FragmentManager 的参考.如果任何机构可以揭示一些隐藏的事实以避免此类问题:

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)  

推荐答案

这是我目前遇到的最愚蠢的错误.我有一个 Fragment 应用程序非常适合 API <11Force ClosingAPI > 11 上.

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.

我真的无法弄清楚它们在调用 saveInstance 时在 Activity 生命周期内发生了什么变化,但我在这里解决了这个问题:

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.
}

我只是不调用 .super() 并且一切正常.我希望这会为您节省一些时间.

I just do not make the call to .super() and everything works great. I hope this will save you some time.

经过更多研究,这是一个已知的 错误.

after some more research, this is a known bug in the support package.

如果您需要保存实例,并向您的 outState Bundle 添加一些内容,您可以使用以下内容:

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);
}

如果您在 Activity 在后台消失后尝试执行交易,也可能会发生这种情况.为了避免这种情况,您应该使用 commitAllowingStateLoss()

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()

以上解决方案修复了我记得的早期 support.v4 库中的问题.但如果您对此仍有问题,您必须还阅读@AlexLockwood 的博客:Fragment Transactions &活动状态损失

博文摘要(但我强烈建议您阅读):

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()onPause() 之后的交易在 pre-Honeycomb 和 onStop() 之后后蜂窝
  • Activity 生命周期方法中提交事务时要小心.使用 onCreate(), onResumeFragments()onPostResume()
  • 避免在异步回调方法中执行事务
  • 仅将 commitAllowingStateLoss() 用作最后的手段
  • NEVER commit() transactions after onPause() on pre-Honeycomb, and onStop() on post-Honeycomb
  • Be careful when committing transactions inside Activity lifecycle methods. Use onCreate(), onResumeFragments() and onPostResume()
  • Avoid performing transactions inside asynchronous callback methods
  • Use commitAllowingStateLoss() only as a last resort

这篇关于获取异常“IllegalStateException:onSaveInstanceState 后无法执行此操作";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆