Fragment innerhalb des Fragments

Ich brauche Hilfe bei arbeiten auf fragment im inneren-fragment, eigentlich habe ich
bin mit einem problem konfrontiert, auf drücken der zurück-Taste. Anwendung Hauptfenster
hat Tasten und drücken jeder Taste anzeigen ersetzen mit neuen
- fragment und das fragment enthalten, die in einem anderen fragment),
dynamisch hinzufügen/ersetzen-fragment ist in Ordnung, durch drücken
button1-fragment ersetzt, das gleiche passiert beim drücken der Taste, aber wenn
I drücken Sie die Taste erneut, bekam eine Ausnahme:

"Duplicate id 0x7f05000a, tag null, or parent id 0x7f050009 with
another fragment for com........ fragmentname"

bedeutet "fragment" oder " innere Fragmente sind bereits aufgenommen und ich bin versucht
fügen Sie Sie wieder, niemand hat Ahnung, wie das arbeiten mit fragment-innen
fragment und hin und her zu bewegen, ohne jedes problem, danke für die
Unterstützung.

MainActivity, wo Fragmente werden dynamisch Hinzugefügt und
ersetzt.

   public class FragmentInsideFragmentTestActivity extends Activity {

   private Button button1;
   private Button button2;
   private Button button3;
   private Button button4;


   /** Called when the activity is first created. */
  @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   button1 =(Button) this.findViewById(R.id.button1);
   button1.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           onButtonClick(view);

           }
   });

   button2 =(Button) this.findViewById(R.id.button2);
   button2.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           onButtonClick(view);

           }
   });

   button3 =(Button) this.findViewById(R.id.button3);
   button3.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           onButtonClick(view);

           }
   });

   button4 =(Button) this.findViewById(R.id.button4);
   button4.setOnClickListener(new View.OnClickListener() {
       public void onClick(View view) {
           onButtonClick(view);

           }
   });

    }

   public void onButtonClick(View v) {
           Fragment fg;
           switch (v.getId()) {
           case R.id.button1:
                   fg=FirstFragment.newInstance();
                   replaceFragment(fg);
                   break;
           case R.id.button2:
                   fg=SecondFragment.newInstance();
                   replaceFragment(fg);
                   break;
           case R.id.button3:
                   fg=FirstFragment.newInstance();
                   replaceFragment(fg);
                   break;
           case R.id.button4:
                   fg=SecondFragment.newInstance();
                   replaceFragment(fg);
                   break;
           }
   }

   private void replaceFragment(Fragment newFragment) {

   FragmentTransaction trasection =
   getFragmentManager().beginTransaction();
   if(!newFragment.isAdded()){
           try{
                   //FragmentTransaction trasection =
           getFragmentManager().beginTransaction();
           trasection.replace(R.id.linearLayout2, newFragment);
           trasection.addToBackStack(null);
           trasection.commit();

           }catch (Exception e) {
                           //TODO: handle exception
                    //AppConstants.printLog(e.getMessage());

                   }
   }else
           trasection.show(newFragment);

      }
    }

Hier ist das Layout: main.xml

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

   <LinearLayout android:id="@+id/linearLayout1"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:orientation="horizontal">
           <Button android:layout_width="wrap_content"
                   android:id="@+id/button1"
                   android:layout_height="wrap_content"
                   android:text="Button1"></Button>
           <Button android:text="Button2"
                   android:id="@+id/button2"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"></Button>
           <Button android:text="Button3"
                   android:id="@+id/button3"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"></Button>
           <Button android:text="Button4"
                   android:id="@+id/button4"
                   android:layout_width="wrap_content"
                   android:layout_height="wrap_content"></Button>
   </LinearLayout>

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

Hoffe, dass ich versucht klar mein problem.

Kommentar zu dem Problem - Öffnen
obige code funktioniert für mich vollkommen in Ordnung mit android 3.1 Kommentarautor: Vivek
mögliche Duplikate von Fragmente in Fragmenten Kommentarautor: Vladimir
für mehr details Lesen Sie bitte stackoverflow.com/a/11020531/1219456 Kommentarautor: Raneez Ahmed

InformationsquelleAutor der Frage Ijaz Ahmed | 2011-07-12

Schreibe einen Kommentar