Android 调用FragmentTransaction.Commit()时发生异常
我正在尝试做一些应该简单的事情,我的主要布局是这样的Android 调用FragmentTransaction.Commit()时发生异常,android,android-layout,android-fragments,Android,Android Layout,Android Fragments,我正在尝试做一些应该简单的事情,我的主要布局是这样的 <?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=
<?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;
}
@覆盖
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
//加载机架磁贴的布局
视图=充气机。充气(R.layout.grid_片段,null,false);
//获取根线性布局以向其中添加行
LinearLayout root=(LinearLayout)view.findViewById(R.id.gridFragmentMainContainer);
//构建网格的行
int rowsId=21;
对于(int i=0;i<15;i++){
//创建表示网格中的行的线性布局
LinearLayout行=新的LinearLayout(this.getActivity());
row.setId(rowsId++);
行。设置方向(线性布局。水平);
row.setLayoutParams(新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,10,1.0f));
//构建网格的单元
对于(int j=0;j<15;j++){
//创建细胞片段
CellFragment CellFragment=新的CellFragment();
//将单元格片段添加到行片段中
this.getFragmentManager().beginTransaction().add(row.getId(),cellFragment.commit();
}
//将行添加到根元素
root.addView(行);
}
返回视图;
}
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:无法启动活动组件信息{io.Raindesce.kalimat/io.Raindesce.kalimat.test.KalimatActivity}:java.lang.IllegalArgumentException:未找到片段CellFragment{4103c790#1 id=0x7f050005}的id 0x7f050005的视图
我已经寻找这个问题的原因和解决方案两天了。需要创建
活动
(并保存其状态),以便执行碎片事务
,否则您将得到异常。请尝试在ActivityCreated上执行您的事务。您的代码还尝试在片段中嵌入片段。您不应该需要实例化新的cellfragment并尝试将其添加到父视图中。这可能就是为什么会出现异常。。。片段中不能存在片段。你应该考虑阅读开发者网站上的片段。我猜是这样的,你不能在其他片段中设置片段,谢谢。