Ich möchte eine Activity
erstellen, die eine Art von Menü zeigt, die ein Benutzer durchlaufen kann. Durch Klicken auf ein Element wird ein neuer Bildschirm angezeigt, der dem Benutzer weitere Optionen bietet (Assistenten-ähnlich).Wechseln zwischen Fragmenten in einer einzigen Aktivität
Ich wollte dies mit Fragment
s implementieren, aber es funktioniert nicht für mich.
Im Moment habe ich:
main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/main_fragmentcontainer" >
<fragment
android:id="@+id/mainmenufragment"
android:name="com.myapp.MainMenuFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<fragment
android:id="@+id/secondmenufragment"
android:name="com.myapp.SecondMenuFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainMenuFragment
mit einem OnClickListener
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.mainmenu, container, false);
setupButton(view);
return view;
}
/* Button setup code omitted */
@Override
public void onClick(View v) {
SherlockFragment secondRunMenuFragment = (SherlockFragment) getSherlockActivity().getSupportFragmentManager().findFragmentById(R.id.secondmenufragment);
FragmentTransaction transaction = getSherlockActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, secondMenuFragment); //also crashes with R.id.main_fragmentcontainer
transaction.addToBackStack(null);
transaction.commit();
}
Nun, wenn ich die Taste drücken, stürzt die Anwendung mit diesem logcat:
06-27 01: 45: 26.309: E/AndroidRuntime (8747): java.lang.IllegalStateException: Die Container-ID des Fragments SecondMenuFragment {405e2a70 # 1 id = 0x7f060029} konnte nicht geändert werden: war 2131099689 jetzt 2131099687 06-27 01: 45: 26.309: E/AndroidRuntime (8747): bei android.support.v4.app.BackStackRecord.doAddOp (unbekannte Quelle)
06-27 01: 45: 26.309: E/AndroidRuntime (8747): bei android.support.v4.app.BackStackRecord.replace (Unbekannte Quelle)
06-27 01: 45: 26.309: E/AndroidRuntime (8747): bei android.support.v4.app.BackStackRecord.replace (Unbekannte Quelle)
06-27 01: 45: 26.309: E/AndroidRuntime (8747): unter com.myapp.MainMenuFragment $ MyButtonOnClickListener.onClick (MainMenuFragment.java:52)
Was mache ich falsch?
[Es gibt eine ähnliche Frage hier] (http://stackoverflow.com/questions/9092048/swap-two-fragment- gleichzeitig) – adneal
Wenn Sie versuchen, einen Assistenten zu erstellen, was ist der zweite '' für? –
CommonsWare
Ich habe diese Links tatsächlich gesehen, aber ich weiß nicht, wie man sie in meinem Fall anwendet. – nhaarman