我正在尝试创建一个片段,该片段具有将子片段添加到自身的公共(public)方法。

我一直在阅读可能类似的问题,但到目前为止没有找到任何帮助。我已将问题简化为如下所示的简单测试应用程序。

fragA添加到主布局后,我调用公共(public)方法fragA.addFragB()使其获取向其自身添加FragmentClassB实例的方法,但这导致测试应用程序崩溃,指示“ Activity 已被破坏”(请参阅​​最后的LogCat的职位)。这是否意味着fragA已被破坏,所以我无法向其中添加fragB,还是意味着fragB已被破坏,因此无法将其添加至fragA?还是完全意味着别的意思?

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragMan = getSupportFragmentManager();

        // add Fragment A to the main linear layout
        FragmentTransaction fragTrans = fragMan.beginTransaction();
        FragmentClassA fragA = new FragmentClassA();
        fragTrans.add(R.id.mainLinearLayout, fragA);
        fragTrans.addToBackStack("A");
        fragTrans.commit();

        // get Fragment A to add a Fragment B to itself
        fragA.addFragB();
    }

}

activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/mainLinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="horizontal" >
    </LinearLayout>

</RelativeLayout>

FragmentClassA.java
public class FragmentClassA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_a, container, false);
    }

    public void addFragB() {
        FragmentManager childFragMan = getChildFragmentManager();

        FragmentTransaction childFragTrans = childFragMan.beginTransaction();
        FragmentClassB fragB = new FragmentClassB();
        childFragTrans.add(R.id.fragA_LinearLayout, fragB);
        childFragTrans.addToBackStack("B");
        childFragTrans.commit();

    }
}

fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragA_LinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >


</LinearLayout>

FragmentClassB.java
public class FragmentClassB extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_b, container, false);
    }

}

fragment_b.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

LogCat
11-18 16:17:05.627: E/AndroidRuntime(14351): FATAL EXCEPTION: main
11-18 16:17:05.627: E/AndroidRuntime(14351): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nestedfragmenttest/com.example.nestedfragmenttest.MainActivity}: java.lang.IllegalStateException: Activity has been destroyed
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.os.Looper.loop(Looper.java:137)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.main(ActivityThread.java:5103)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at java.lang.reflect.Method.invokeNative(Native Method)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at java.lang.reflect.Method.invoke(Method.java:525)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at dalvik.system.NativeStart.main(Native Method)
11-18 16:17:05.627: E/AndroidRuntime(14351): Caused by: java.lang.IllegalStateException: Activity has been destroyed
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at com.example.nestedfragmenttest.FragmentClassA.addFragB(FragmentClassA.java:26)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at com.example.nestedfragmenttest.MainActivity.onCreate(MainActivity.java:25)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.Activity.performCreate(Activity.java:5133)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-18 16:17:05.627: E/AndroidRuntime(14351):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-18 16:17:05.627: E/AndroidRuntime(14351):    ... 11 more

编辑:

ianhanniballake的答案和GrIsHu的后来的答案在同一点上扩展,都有助于指出问题的根源。但是,这引起了另一个问题。
最终目的是FragmentClassA将成为库的一部分。它会在多种情况下使用,并且FragmentClassB实例的数量会有所不同,甚至可能没有。因此,我需要能够从父 Activity 中触发将子片段添加到FragmentClassA的任何实例。我刚刚看过将fragA保留为MainActivity中的类级别变量,然后在fragA.AddFragB()MainActivity方法中调用onActivityCreated(),但它不可被覆盖。有什么想法吗?

最佳答案

您不能直接加载在FragA中声明的片段。首先将加载FragmentA,然后再通过从addFragB() fragA's方法调用onCreateView()方法来加载FragmentB。

尝试如下:

从您的 MainActivity 中删除fragA.addFragB();

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragMan = getSupportFragmentManager();

        // add Fragment A to the main linear layout
        FragmentTransaction fragTrans = fragMan.beginTransaction();
        FragmentClassA fragA = new FragmentClassA();
        fragTrans.add(R.id.mainLinearLayout, fragA);
        fragTrans.addToBackStack("A");
        fragTrans.commit();

    }

}

并尝试从FragmentB加载FragmentA,如下所示:
public class FragmentClassA extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        addFragB(); //Call and load fragment B here.
        return inflater.inflate(R.layout.fragment_a, container, false);
    }

    public void addFragB() {
        FragmentManager childFragMan = getChildFragmentManager();

        FragmentTransaction childFragTrans = childFragMan.beginTransaction();
        FragmentClassB fragB = new FragmentClassB();
        childFragTrans.add(R.id.fragA_LinearLayout, fragB);
        childFragTrans.addToBackStack("B");
        childFragTrans.commit();

    }
}

10-08 15:52