我正在尝试做一些应该简单的事情,我的主要布局是这样的
<?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-08 03:36:27.414: E/AndroidRuntime(835): java.lang.RuntimeException: Unable to start activity ComponentInfo{io.raindance.kalimat/io.raindance.kalimat.test.KalimatActivity}: java.lang.IllegalArgumentException:没有为片段 CellFragment {4103c790 #1 id=0x7f050005} 找到 id 0x7f050005 的视图
我一直在寻找这个问题的原因和解决方案 2 天。