Android “非法国家例外”;“活动已销毁”;在片段(嵌套片段)中添加片段

Android “非法国家例外”;“活动已销毁”;在片段(嵌套片段)中添加片段,android,android-fragments,nested,fragment,android-fragmentactivity,Android,Android Fragments,Nested,Fragment,Android Fragmentactivity,基本上,这个问题已经被问了很多次了。给出的解决方案对我来说不起作用。在某些情况下,用户未发布相应的答案。 我想要嵌套片段。我的设想如下: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragContainer" android:layout_width="match_parent" android:layout_height="match_parent

基本上,这个问题已经被问了很多次了。给出的解决方案对我来说不起作用。在某些情况下,用户未发布相应的答案。 我想要嵌套片段。我的设想如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/fragContainer"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:text="product1" />
</LinearLayout>
我有一个主要活动ProductEnterFrag和ProductListFrag ProductDetailFrag

我已经通过FragmentManager将ProductEnterFrag动态添加到MainActivity。像这样:

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, new ProductEnterFrag());
ft.commit();
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ProductListFrag(new ProductListFrag());
}

public void ProductListFrag(SherlockFragment frag) {
    getChildFragmentManager().beginTransaction().add(R.id.fragContainer, frag).commit();
    getChildFragmentManager().executePendingTransactions();`
ProductEnter布局如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/fragContainer"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:text="product1" />
</LinearLayout>
}

ProductListFrag的布局如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/fragContainer"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:text="product1" />
</LinearLayout>

现在单击按钮,我想添加另一个片段,比如ProductDetailFrag,它应该是另一个子片段。 当我尝试从ProductListFrag添加ProductDetailFrag时,如下所示:

FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, new ProductEnterFrag());
ft.commit();
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ProductListFrag(new ProductListFrag());
}

public void ProductListFrag(SherlockFragment frag) {
    getChildFragmentManager().beginTransaction().add(R.id.fragContainer, frag).commit();
    getChildFragmentManager().executePendingTransactions();`
newproductEnterFrag().ProductListFrag(newproductDetailsFrag())

它向我抛出:
java.lang.IllegalStateException:活动已被销毁


我是一个新的嵌套片段。请帮助我哪里做错了。我在过去两天一直在处理这个问题

添加
ProductDetailsFrag
时,是否有理由创建
ProductEnterFrag
的新实例?该新实例未附加到某个活动,因此任何片段事务都将失败。@Luksprog那么我应该如何从ProductEnterFragmg访问该函数,该函数创建将片段添加到childFragmentManager。我是新来的。可能是我做错了什么。我不知道你的代码,但我假设你正在调用
newproductenterfrag().ProductListFrag(newproductdetailsfrag())
ProductListFrag
中的某个位置。在
ProductListFrag
中,您确实通过
getParentFragment()
引用了包装器
ProductEnterFrag
,因此如果您在
ProductListFrag
中,请执行:
((ProductEnterFrag).getParentFragment()).ProductListFrag(新产品详细信息框架())@Luksprog吨thnks。。。它起作用了。但我有一个疑问,当我从ProductDetailFrag返回时,我应该转到ProductListFrag。但这让我不再申请了。我如何处理这个问题。您的
ProductListFrag
方法没有使用
addToBackStack(null)
将事务添加到backstack。