Activity
を作成して、ユーザーが通過できるメニューを表示したいとします。項目をクリックすると、新しい画面が表示され、ユーザーはより多くのオプションを選択できます(ウィザードのような)。単一のアクティビティでフラグメント間を切り替える
これはFragment
を使用して実装したいと思っていましたが、うまく動作しません。
今私が持っている:
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
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(); }
私は、ボタン、このlogcatを使用してアプリケーションのクラッシュを押すと、
06-27 01:45:26.309: E/AndroidRuntime(8747):java.lang.IllegalStateException:フラグメントのコンテナIDを変更できません。SecondMenuFragment {405e2a70#1 id = 0x7f060029}:2131099687 now 2131099687
06-27 01:45:26.309:E/AndroidRuntime 8747):android.support.v4.app.BackStackRecord.doAddOp(不明なソース)
06-27 01:45:26.309:E/AndroidRuntime(8747):android.support.v4.app.BackStackRecord.replace不明なソース)
06-27 01:45:26.309:E/AndroidRuntime(8747):android.support.v4.app.BackStackRecord.replace(不明なソース)
06-27 01:45:26.309:E/AndroidRuntime(8747):com.myapp.MainMenuFragment $ MyButtonOnClickListener.onClick(MainMenuFragment.java:52)
私は間違って何をしていますか?
[ここに同様の質問があります](http://stackoverflow.com/questions/9092048/swap-two-fragment-同時に) – adneal
ウィザードを作成しようとしている場合、2番目の「」は何ですか? –
CommonsWare
私はこれらのリンクを実際に見たことがありますが、私の場合はどのように適用するのか分かりません。 – nhaarman