0

シンプルなことをしようとしています。メインのレイアウトはこんな感じです。

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

    <fragment
        android:name="io.raindance.kalimat.grid.GridFragment"
        android:id="@+id/gridControlInMainView" 
        android:layout_width="match_parent"
        android:layout_height="match_parent" />        

</LinearLayout>

メインアクティビティにはJavaコードはありません。これで、GridFragmentのコードとレイアウトは次のようになります。

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

</LinearLayout>

Javaコード:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Load the layout of the rack tile
    View view = inflater.inflate(R.layout.grid_fragment, null, false);



    // Get the root linear layout to add the rows to it
        LinearLayout root = (LinearLayout) view.findViewById(R.id.gridFragmentMainContainer);

        // Build the rows of the grid
            int rowsId = 21;
        for (int i = 0; i < 15; i++) {
            // Create the linear layout that represents a row in the grid
            LinearLayout row = new LinearLayout(this.getActivity());
            row.setId(rowsId++);
            row.setOrientation(LinearLayout.HORIZONTAL);

            row.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 10, 1.0f));

            // Build the cells of the grid
            for (int j = 0; j < 15; j++) {
                // Create the cell fragment
                CellFragment cellFragment = new CellFragment();
            // Add the cell fragment to the row fragment
                            this.getFragmentManager().beginTransaction().add(row.getId(), cellFragment).commit();
}
        // Add the row to the root element
        root.addView(row);
    }

    return view;
}

CellFragmentのレイアウトは以下のとおりであり、Javaコードはありません。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/lll1"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="match_parent">

</LinearLayout>

アプリケーションを実行すると、commit()呼び出しが原因で例外が発生します。これは、commit()呼び出しを削除しても例外が発生せず、とにかく行が正常に作成され、階層に追加されているためです。

以下の例外が発生します07-0803:36:27.414:E / AndroidRuntime(835):java.lang.RuntimeException:アクティビティを開始できませんComponentInfo {io.raindance.kalimat / io.raindance.kalimat.test.KalimatActivity}: java.lang.IllegalArgumentException:フラグメントCellFragment {4103c790#1 id=0x7f050005}のID0x7f050005のビューが見つかりません

私はこの問題の理由と解決策を2日間探していました。

4

1 に答える 1

0

を実行するには、を作成(およびそのActivity状態を保存)する必要FragmentTransactionがあります。そうしないと、例外が発生します。onActivityCreated代わりにでトランザクションを実行してみてください。

于 2012-07-08T08:26:10.277 に答える