11

Activityユーザーが通過できる一種のメニューを表示するを作成したいと思います。アイテムをクリックすると、新しい画面が表示され、ユーザーはより多くのオプションを使用できます (ウィザードのように)。

sを使用してこれを実装したかった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>

MainMenuFragmentOnClickListener

@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: フラグメント SecondMenuFragment のコンテナー ID を変更できませ
ん: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) で

私は何を間違っていますか?

4

3 に答える 3

16

<fragment>個人的には、要素はありません。

ステップ1:アクティビティレイアウト<FrameLayout>に、ウィザードの可変部分のと、さまざまなボタンを入力します。

ステップ2:onCreate()アクティビティの中で、を実行しFragmentTransactionて最初のウィザードページをにロードしますFrameLayout

ステップ3:「次へ」クリックで、を実行して、の内容をウィザードの次のページにFragmentTransaction置き換えます。FrameLayout

ステップ4:ボタンが使用できないときにボタンを無効にするための適切なスマートを追加します(たとえば、最初のウィザードページに戻ります)。

また、ウィザードの画面上の「戻る」ボタンと組み合わせて、[戻る]ボタンがどのように機能するかを検討する必要があります。両方を同じように動作させたい場合は、各トランザクションをバックスタックに追加し、「戻る」ボタンを処理するときにバックスタックからコンテンツをポップする必要があります。

いつの日か、誰も私に勝てない場合は、フラグメントごとのウィザードの例、またはおそらく再利用可能なコンポーネントを作成しようとします。

于 2012-06-26T23:59:38.757 に答える