我有3个片段标签,fragment1,fragment2和fragment3。我在fragment2中创建了另一个片段选项卡。问题是每当我重新选择包含片段选项卡的fragment2时,我的应用程序崩溃并显示如下所示的LOGCAT。我已经在此链接Getting the error "Java.lang.IllegalStateException Activity has been destroyed" when using tabs with ViewPager上的stackoverflow中关注了一个问题,但它似乎也无效。另外,我是否导入了正确的库“import java.lang.reflect.Field;
”?
fragment2.java
import com.actionbarsherlock.app.SherlockFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment2 extends SherlockFragment{
private FragmentTabHost mTabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getSherlockActivity());
mTabHost.setup(getSherlockActivity(), getChildFragmentManager(), R.layout.fragmenttab2);
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator("Simple"),
Fragment1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator("Contacts"),
Fragment2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator("Custom"),
Fragment3.class, null);
return mTabHost;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mTabHost = null;
}
}
LogCat:
07-22 16:18:50.668: E/AndroidRuntime(29774): FATAL EXCEPTION: main
07-22 16:18:50.668: E/AndroidRuntime(29774): java.lang.IllegalStateException: Activity has been destroyed
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:278)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.view.View.dispatchAttachedToWindow(View.java:9953)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2198)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2206)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.view.ViewGroup.addViewInner(ViewGroup.java:3376)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.view.ViewGroup.addView(ViewGroup.java:3208)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.view.ViewGroup.addView(ViewGroup.java:3165)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.view.ViewGroup.addView(ViewGroup.java:3145)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:922)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.os.Handler.handleCallback(Handler.java:605)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.os.Handler.dispatchMessage(Handler.java:92)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.os.Looper.loop(Looper.java:137)
07-22 16:18:50.668: E/AndroidRuntime(29774): at android.app.ActivityThread.main(ActivityThread.java:4507)
07-22 16:18:50.668: E/AndroidRuntime(29774): at java.lang.reflect.Method.invokeNative(Native Method)
07-22 16:18:50.668: E/AndroidRuntime(29774): at java.lang.reflect.Method.invoke(Method.java:511)
07-22 16:18:50.668: E/AndroidRuntime(29774): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
07-22 16:18:50.668: E/AndroidRuntime(29774): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
07-22 16:18:50.668: E/AndroidRuntime(29774): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
编写此代码时:
Field childFragmentManager = Fragment2.class.getDeclaredField("mChildFragmentManager");
表示您想获得在Fragment2中声明的文件。
我可以在Fragment2中看到mChildFragmentManager字段。
如果你确定这个档案是在超级班级宣布的,你应该使用 的 Fragment2.class.getField 强>
答案 1 :(得分:0)
试试 childFragmentManager
childeFragmentManager = getChildFragmentManager().
在4.2版本发布后,它被添加到支持库中,因为之前不支持嵌套片段。
来源 http://developer.android.com/about/versions/android-4.2.html#NestedFragments