3

我正在使用嵌套片段并且片段加载良好。但是当我在这个 frameLayout 中加载另一个片段并想再次加载嵌套片段时,我的应用程序崩溃了。

07-14 16:58:21.272: E/AndroidRuntime(1813): java.lang.IllegalStateException: Activity has been destroyed
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1342)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at me.kaidul.uhunt.Fragment1.onCreateView(Fragment1.java:23)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-14 16:58:21.272: E/AndroidRuntime(1813):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)

我的父片段类:

public class Fragment1 extends SherlockFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment1, container, false);
        Fragment fragmentC = new HeadlinesFragment();
        Fragment frag = new Child();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.child_fragment, fragmentC );
        transaction.add(R.id.child_fragment2, frag );
        transaction.commit();
        return rootView;
    }

}

编辑:这就是我将所有片段添加到活动的方式:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        // Locate Position
        switch (position) {
        case 0:
            ft.replace(R.id.content_frame, fragment1);
            break;
        case 1:
            ft.replace(R.id.content_frame, fragment2);
            break;
        case 2:
            ft.replace(R.id.content_frame, fragment3);
            break;
        }
        ft.commit();
4

1 回答 1

6

这与嵌套片段的已知错误有关。这是解决它的一种方法 - https://stackoverflow.com/a/15656428/574785

于 2013-07-14T15:14:50.363 回答