处理和缓解IllegalStateException异常(QUOT;未能保存状态:有效$碎片已清除指数:-1") [英] Handling and Mitigating IllegalStateException ("failure saving state: active $Fragment has cleared index: -1")

查看:820
本文介绍了处理和缓解IllegalStateException异常(QUOT;未能保存状态:有效$碎片已清除指数:-1")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Andr​​oid应用程序管理多个片段。我看到了大量的撞车现场,然而,含有该日志行:

My Android application manages multiple fragments. I am seeing a large number of crashes in the field, however, containing this log line:

java.lang.IllegalStateException:失败节能状态:活跃XxxFragment {81e598 ID = 0x7f0b0069 tag_yyy}已清除指数:-1

java.lang.IllegalStateException: Failure saving state: active XxxFragment{81e598 id=0x7f0b0069 tag_yyy} has cleared index: -1

在堆栈溢出的答案搜索已经无果而终;我似乎有很多企业在琢磨究竟这是什么意思例外。挖掘到异常跟踪和Android的来源,我可以看到异常是从它在我的主要活动是保存其状态(FragmentActivity.onSaveInstanceState)点来了,个别片段被写入到Parcelable。每个片段都有一个索引(称为MINDEX),它必须为非负,但它不是从code真正清楚为什么一定是这样的,因为MINDEX从未在该函数再次使用。

Searches of answers in Stack Overflow have been fruitless; I seem to have a lot of company in wondering exactly what this exception means. Digging into the exception trace and Android sources, I can see that the exception is coming from the point at which my main Activity is saving its state (FragmentActivity.onSaveInstanceState), and the individual Fragments are being written into a Parcelable. Each Fragment has an index (called mIndex) which must be nonnegative, but it's not really clear from the code why that must be the case, as mIndex is never used again in that function.

我没有好主意片段是如何进入这个状态,或者我可以做些什么。我一直无法重现错误在我自己的测试环境。任何人都可以摆脱任何光线如何避免和/或处理该异常?

I have no good idea how the Fragment gets into this state, or what I can do about it. I have been unable to reproduce the error in my own test environment. Can anyone shed any light on how to avoid and/or deal with this exception?

<一个href=\"http://stackoverflow.com/questions/19098026/java-lang-illegalstateexception-failure-saving-state-active-has-cleared-index\">java.lang.IllegalStateException:故障保存状态:活跃已清除的片段指数

<一个href=\"http://stackoverflow.com/questions/15533463/what-does-active-fragment-has-cleared-index-1-mean-and-how-do-i-fix-it\">What没有活性片段已被清除指数:-1意思,如何解决呢

<一个href=\"http://stackoverflow.com/questions/23631528/getting-exception-as-failure-saving-state-active-fragment-has-cleared-index-1\">Getting例外,因为失败节能状态:活性片段已被清除指数:-1当我的Andr​​oid设备的pressing home键

<一个href=\"http://stackoverflow.com/questions/19935400/illegalstateexception-with-android-fragments\">IllegalStateException与Android碎片

推荐答案

我要保留我的previous意见,使这个实际的答案。 setRetainInstance(真)是一个红鲱鱼。至少在我的情况。从这里开始:<一href=\"http://www.localwisdom.com/blog/2013/03/android-error-java-lang-illegalstateexception-failure-saving-state-active-fragmentname/\" rel=\"nofollow\">http://www.localwisdom.com/blog/2013/03/android-error-java-lang-illegalstateexception-failure-saving-state-active-fragmentname/

I'm going to redact my previous comment and make this an actual answer. setRetainInstance(true) was a red herring. At least in my case. Start here: http://www.localwisdom.com/blog/2013/03/android-error-java-lang-illegalstateexception-failure-saving-state-active-fragmentname/

你最有可能试图进行的交易片段,你没有提及片段的正确实例

"You most likely tried to perform a fragment transaction where you didn’t have reference to the correct instance of a fragment"

当我读到这一切对我来说是有意义的。我是做与片段2的错误的东西。

When I read that it all made sense for me. I was doing 2 wrong things with fragments.


  1. 我违规片段单身。 (主要禁忌)

  2. 我试图用活动穿过这些相同的片段实例,而不正确的保存状态。 (可通过这里提到的方法来实现:<一href=\"http://stackoverflow.com/a/12465343/333525\">http://stackoverflow.com/a/12465343/333525)

  1. My offending fragments were singletons. (Major no-no)
  2. I was trying to use these same fragment instances across Activities without correctly saving state. (Which can be achieved by methods mentioned here: http://stackoverflow.com/a/12465343/333525)

(希望这可以帮助,这是只是一个评论太长。)

(Hope this helps. It was too long for just a comment.)

这篇关于处理和缓解IllegalStateException异常(QUOT;未能保存状态:有效$碎片已清除指数:-1&QUOT;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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