0

Wat I DID:我有两个碎片。将显示左侧列表。将创建基于列表选定片段的右侧。

预期输出:当我单击右侧片段上的按钮时。应该关闭片段并显示其他片段

发生错误 :Error ::java.lang.IllegalStateException: Activity 已被销毁

public void manageFragment(int id, int fragmentId, boolean backStack) {

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();

    switch (id) {

    case 0:
        One o = new One();
        fragmentTransaction.remove(getSupportFragmentManager().findFragmentByTag(
                Tag));
        fragmentTransaction.add(fragmentId, o, "one");
        Tag = "one";
        break;
    case 1:
        Two t = new Two();
        fragmentTransaction.remove(getSupportFragmentManager().findFragmentByTag(
                Tag));
        fragmentTransaction.add(fragmentId, t, "two");
        Tag = "two";
        break;
    case 2:
        Three thr = new Three();
        fragmentTransaction.remove(getSupportFragmentManager().findFragmentByTag(Tag));
        fragmentTransaction.add(fragmentId, thr, "three");
        Tag = "three";
        break;
    case 3:
        Four f = new Four();
        fragmentTransaction.remove(getSupportFragmentManager().findFragmentByTag(
                Tag));
        fragmentTransaction.add(fragmentId, f, "four");
        Tag = "four";
        break;
    case 4:

        Five fiv = new Five();
        fragmentTransaction.remove(getSupportFragmentManager().findFragmentByTag(
                Tag));
        fragmentTransaction.add(fragmentId, fiv, "five");
        Tag = "five";
        break;
    }
    if(backStack){
        fragmentTransaction.addToBackStack(null);
    }

    fragmentTransaction.commit();
}
public void setDetails(int id) {
    if (findViewById(R.id.listFragmenttablet) != null) {
        manageFragment(id, R.id.detailsFragmenttablet, false);

    } else {
        removeListFragment();
        manageFragment(id, R.id.Fragmentphone, true);
    }

}

这是 MainActivity 类。我将在单击左侧列表时调用 setDetails 函数。基于位置右侧片段会改变。

public class One extends Fragment {  

 @Override  
 public View onCreateView(LayoutInflater inflater, final ViewGroup container,  
           Bundle savedInstanceState) {  

     View view =inflater.inflate(R.layout.one, container, false);

     Button bt=(Button)view.findViewById(R.id.onelayout);
      bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                simplemethod() ;
            }


        });
      return view; 
 }  
 private void simplemethod() {
        // TODO Auto-generated method stub
//  getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
    new MainActivity().manageFragment(2, R.id.detailsFragmenttablet, false);
    //new MainActivity().setDetails(3);

    }

这是详细片段。当我单击此按钮时,它应该删除并且应该显示另一个片段

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/listFragmenttablet"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="2"
    android:orientation="horizontal" />

<LinearLayout
    android:id="@+id/detailsFragmenttablet"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="3"
    android:orientation="horizontal" >
</LinearLayout>

这是 MainLayout 布局文件

4

0 回答 0