2013-01-31 24 views
12

Ich habe eine Aktivität mit Button darauf und wenn ein Klick ich das Fragment mit einem anderen Button auf Fragment aufrufen. aber wenn ich auf den Knopf des Fragments klicke, kann ich kein zweites Fragment nennen. dies ist meine Quelle, recht einfach: activity_main.xml:Call-Fragment von Fragment

<LinearLayout 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:orientation="horizontal" 
tools:context=".MainActivity" > 

<Button 
    android:id="@+id/btn_click" 
    android:text="Call Fragment" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    android:onClick="onClick" 
    /> 
</LinearLayout> 

fragment1.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:background="#f0f0f0" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/fragment1" 
    android:text="Fragment 1" 
    android:textSize="25sp" 
    android:gravity="center" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    /> 

<Button 
    android:id="@+id/btn_frag2" 
    android:text="Call Fragment 2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

fragment2.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:background="#f0f0f0" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/fragment2" 
    android:text="Fragment 2" 
    android:textSize="25sp" 
    android:gravity="center_vertical|center_horizontal" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    /> 
</LinearLayout> 

MainActivity.java

public class MainActivity extends Activity { 

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

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.activity_main, menu); 
    return true; 
} 

public void onClick(View v) { 
    Fragment1 fragment1 = new Fragment1(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(android.R.id.content, fragment1); 
    fragmentTransaction.commit(); 
} 

} 

Fragment1. java

public class Fragment1 extends Fragment { 

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

    public void onClick2(View view) { 
     Fragment2 fragment2 = new Fragment2(); 
     FragmentManager fragmentManager = getFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment1, fragment2); 
     fragmentTransaction.commit(); 
    } 
} 

Fragment2.java

public class Fragment2 extends Fragment { 

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

was falsch in meinem Code?

Antwort

24

Ich denke, jetzt Fragment Verschachtelung Update verfügbar ist nur die Rückseite Berechenbarkeit jar

jetzt dig in dem Problem, das es selbst läßt.

public void onClick2(View view) { 
    Fragment2 fragment2 = new Fragment2(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.fragment1, fragment2); 
    fragmentTransaction.commit(); 
} 

ich denke, die R.id.fragment1 zu einem TextView gehört, die kein guter Platz ist in der Kinder Ansichten aufzunehmen, weil es nicht ein ViewGroup, können Sie die textView aus der XML entfernen und ersetzen sie durch eine LinearLayout sagen lässt und es wird funktionieren, wenn nicht sagen Sie mir, was der Fehler ist.

fragment1.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:background="#f0f0f0" 
android:orientation="vertical" > 

<LinearLayout 
    android:id="@+id/fragment1" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" 
    /> 

<Button 
    android:id="@+id/btn_frag2" 
    android:text="Call Fragment 2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

Update für den Fehler in dem Kommentar

public class Fragment1 extends Fragment implements OnClickListener{ 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.fragment1, container, false); 
((Button) v.findViewById(R.id.btn_frag2)).setOnClickListener(this); 
    return v; 
} 

public void onClick(View view) { 
    Fragment2 fragment2 = new Fragment2(); 
    FragmentManager fragmentManager = getFragmentManager(); 
    FragmentTransaction fragmentTransaction =  fragmentManager.beginTransaction(); 
    fragmentTransaction.replace(R.id.container, fragment2); 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
} 

} 
+0

Es funktioniert nicht. Ich ersetze TextView mit LinearLayout, erstes Fragment öffnet sich gut, aber wenn man den Knopf auf dem Fragment drückt, um den zweiten zu öffnen, wird der App-Halt mit Fehler Eine Methode onClick2 (View) in der Aktivitätsklasse com.example.fragments.MainActivity nicht gefunden für onClick-Handler auf Sichtklasse android.widget.Button mit ID 'btn_frag2' –

+0

zeigen Sie mir die Stacktrace des Fehlers – confucius

+1

siehe mein Update .... – confucius

5

Wenn Sie die gesamte Fragment1 mit Fragment2 ersetzen möchten, müssen Sie es innerhalb MainActivity tun, unter Verwendung von: innerhalb einer Methode in MainActivity

Fragment1 fragment2 = new Fragment2(); 
FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
fragmentTransaction.replace(android.R.id.content, fragment2); 
fragmentTransaction.commit(); 

einfach diesen Code setzen, dann wird diese Methode von Fragment1 nennen.

+0

mit diesem, wenn ich das Telefon drehen verlor ich das zweite Fragment, und die erste man kommt auf den Bildschirm –

0

würde ich einen Hörer verwenden. Fragment1 ruft den Hörer (Hauptaktivität)

1

Dies ist meine Antwort, die das gleiche Problem gelöst hat. Fragment2.java ist die Fragmentklasse, die das Layout von fragment2.xml enthält.

public void onClick(View v) { 
        Fragment fragment2 = new Fragement2(); 
        FragmentManager fragmentManager = getFragmentManager(); 
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
        fragmentTransaction.replace(R.id.frame_container, fragment2); 
        fragmentTransaction.addToBackStack(null); 
        fragmentTransaction.commit(); 
       } 
-2

genau das tun: getTabAt (Index Ihres Registerkarte)

 ActionBar actionBar = getSupportActionBar(); 
     actionBar.selectTab(actionBar.getTabAt(0)); 
0

Dieser Code funktioniert für mich, die parent_fragment Methode von child_fragment zu nennen.

ParentFragment-Parent = (ParentFragment) activity.getFragmentManager(). FindViewById (R.id.contaniner);

parent.callMethod();

-1
Fragment fragment = new Fragment(); 
FragmentManager fm = getActivity().getSupportFragmentManager(); 
FragmentTransaction ft = fm.beginTransaction(); 
ft.replace(R.id.content_frame, fragment); 
ft.commit(); 
0

In MainActivity

private static android.support.v4.app.FragmentManager fragmentManager; 

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

    fragmentManager = getSupportFragmentManager();                 


    } 

public void secondFragment() { 

    fragmentManager 
      .beginTransaction() 
      .setCustomAnimations(R.anim.right_enter, R.anim.left_out) 
      .replace(R.id.frameContainer, new secondFragment(), "secondFragmentTag").addToBackStack(null) 
      .commit(); 
} 

In FirstFragment Aufruf SecondFrgment So:

new MainActivity().secondFragment(); 
Verwandte Themen