我有 3 个片段选项卡,片段 1、片段 2 和片段 3。我在 fragment2 中创建了另一个片段选项卡。问题是每当我重新选择包含片段选项卡的片段 2 时,我的应用程序就会崩溃,并显示如下所示的 LOGCAT。我在此链接上的 stackoverflow 中关注了一个问题,当使用带有 ViewPager 的选项卡时出现错误“Java.lang.IllegalStateException Activity 已被破坏”,但它似乎也不起作用。另外,我是否导入了正确的库“ import java.lang.reflect.Field;
”?
片段2.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;
}
}
日志猫:
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)