IllegalStateException异常:其中,MyFragment&GT;是当前不在FragmentManager [英] IllegalStateException: <MyFragment> is not currently in the FragmentManager
问题描述
我知道这听起来像<一个副本href="http://stackoverflow.com/questions/11296411/fragmentstatepageradapter-illegalstateexception-myfragment-is-not-currently">FragmentStatePagerAdapter IllegalStateException异常:其中,MyFragment&GT;是不是目前在FragmentManager 但他的解决办法是不相关的,以我的情况。
I know it sounds like a duplicate of FragmentStatePagerAdapter IllegalStateException: <MyFragment> is not currently in the FragmentManager but his solution isn't relevant to my case.
我收到下列崩溃很少:
了java.lang.RuntimeException:无法暂停活动{MyActivity}:
java.lang.RuntimeException: Unable to pause activity {MyActivity}:
...
致:java.lang.IllegalStateException:片段MyFragment {40648258 ID = 0x7f070051}是不是目前在FragmentManager在 android.support.v4.app.FragmentManagerImpl.putFragment(MT:516)在 android.support.v4.app.FragmentStatePagerAdapter.saveState(MT:185)在 android.support.v4.view.ViewPager.onSaveInstanceState(MT:881)
Caused by: java.lang.IllegalStateException: Fragment MyFragment {40648258 id=0x7f070051} is not currently in the FragmentManager at android.support.v4.app.FragmentManagerImpl.putFragment(MT:516) at android.support.v4.app.FragmentStatePagerAdapter.saveState(MT:185) at android.support.v4.view.ViewPager.onSaveInstanceState(MT:881)
...
在android.view.View.saveHierarchyState(View.java:6238)在 com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1522) 在android.app.Activity.onSaveInstanceState(Activity.java:1138)在 android.support.v4.app.FragmentActivity.onSaveInstanceState(MT:480)在 MyActivity.onSaveInstanceState(MT:336)
at android.view.View.saveHierarchyState(View.java:6238) at com.android.internal.policy.impl.PhoneWindow.saveHierarchyState(PhoneWindow.java:1522) at android.app.Activity.onSaveInstanceState(Activity.java:1138) at android.support.v4.app.FragmentActivity.onSaveInstanceState(MT:480) at MyActivity.onSaveInstanceState(MT:336)
看起来这是奇怪的code我不能明白 FragmentStatePagerAdapter
:
It seems like this is the weird code I can't understand from FragmentStatePagerAdapter
:
for (int i=0; i<mFragments.size(); i++) {
Fragment f = mFragments.get(i);
if (f != null) {
if (state == null) {
state = new Bundle();
}
String key = "f" + i;
mFragmentManager.putFragment(state, key, f);
}
}
它看起来像适配器得到我的片段
从 mFragments
,但不能其状态添加到 FragmentManager
。
It looks like the adapter gets my Fragment
from mFragments
but can't add its state to FragmentManager
.
我已经找不到任何方法来创建此对我的测试设备,只收到了一些用户。
I've couldn't find any way to recreate this on my test devices, only received this from some users.
我使用的是支持包V4。
I'm using support package v4.
任何帮助吗? 谢谢你。
Any help? Thanks.
推荐答案
在FragmentStatePagerAdapter被设置在ViewPager,与片段被填充,但没有被添加到FragmentManager由于活动暂停可能出现这种情况之前布局有发生了ViewPager。这确实击中通过从其它的onCreate活动,与适配器也设置的onCreate。在这种情况下,最好暂缓设置适配器,并将其设置onActivityResult代替。看到这个问题:<一href="https://$c$c.google.com/p/android/issues/detail?id=77285">https://$c$c.google.com/p/android/issues/detail?id=77285
This can happen when a FragmentStatePagerAdapter is set on a ViewPager, with the Fragments being populated, but not yet added to the FragmentManager due to the Activity pausing before layout has occurred for the ViewPager. This is reliably hit by starting another Activity from onCreate, with the Adapter also set in onCreate. In that situation, it's best to hold off on setting the Adapter, and set it onActivityResult instead. See this issue: https://code.google.com/p/android/issues/detail?id=77285
我还提交了一个补丁以检查片段试图保存状态之前,已被添加。
I've also submitted a patch to check for the Fragment having been added before trying to save state.
这篇关于IllegalStateException异常:其中,MyFragment&GT;是当前不在FragmentManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!