造成java.IllegalStateException错误,无活动,导航到片段的第二次,只有当 [英] causing a java.IllegalStateException error, No Activity, only when navigating to Fragment for the SECOND time
问题描述
我得到一个十分不解的错误,我不知道如何甚至开始学习的。
I am getting a very puzzling bug that I have no idea how to even begin working through.
我有一个活动一个简单的应用程序,该意见与片段实施。其中一个片段有它内部的ViewPager;所以我决定,我想用getChildFragmentManager类V4支持库。我还不得不使用ActionBarSherlock,这引起一个问题,因为它不与v4的库的v11的出货。
I have a simple app with one activity, the views are implemented with Fragments. One of the fragments has a ViewPager inside of it; so I decided I that I wanted to use the getChildFragmentManager class of the v4 support library. I also had to use ActionBarSherlock, which caused a problem, because it does not ship with the v11 of the v4 library.
我解决了这个问题通过与V11库替换V4支持库在ABS,一切编译,似乎是工作,包括ViewPager。
I fixed this by replacing the v4 support library in ABS with the v11 library, and everything compiled and appeared to be working, including the ViewPager.
下面是奇怪的一部分:
第一次与ViewPager片段打开,它工作正常;但第二次,导航到,应用程序崩溃,给人一种无用的堆栈跟踪。从调试,我发现这个问题是由getChildFragmentManager返回的FragmentManager;它抛出的无活动错误。
The first time the fragment with the ViewPager opens, it works properly; but the SECOND time it is navigated to, the app crashes, giving a useless stack trace. From debugging, I discovered that the problem was with the FragmentManager returned by getChildFragmentManager; it throws the No Activity error.
没有任何人有任何想法可能会造成什么呢?
Does anybody have any idea what could be causing this?
我会后code,你认为是相关的。
I will post code that you think is relevant.
感谢您, 大卫
推荐答案
我跟着 jeremyvillalobos链接回答(其中是非常有益的),导致我这种解决方法。
I followed the link in jeremyvillalobos answer (which was very helpful) that led me to this workaround.
public class CustomFragment extends Fragment {
private static final Field sChildFragmentManagerField;
static {
Field f = null;
try {
f = Fragment.class.getDeclaredField("mChildFragmentManager");
f.setAccessible(true);
} catch (NoSuchFieldException e) {
Log.e(LOGTAG, "Error getting mChildFragmentManager field", e);
}
sChildFragmentManagerField = f;
}
@Override
public void onDetach() {
super.onDetach();
if (sChildFragmentManagerField != null) {
try {
sChildFragmentManagerField.set(this, null);
} catch (Exception e) {
Log.e(LOGTAG, "Error setting mChildFragmentManager field", e);
}
}
}
...
}
它为我好,而不需要重新实例片段。
It works for me well, without the need to reinstantiate the fragment.
这篇关于造成java.IllegalStateException错误,无活动,导航到片段的第二次,只有当的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!