当我第二次导航到嵌套片段时,应用程序崩溃

时间:2013-07-14 11:04:10

标签: android android-fragments

我正在使用Nested Fragment并且Fragment加载得很好。但是当我在此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)

我的父级Fragment类:

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();

1 个答案:

答案 0 :(得分:6)

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