异常

2012-07-08 56 views
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> 

当我运行应用程序,我得到一个例外,因为提交()的调用,导致异常不会引发当我删除了提交()调用反正行正在成功地创建并添加到层次结构。

我收到以下异常 07-08 03:36:27.414:E/AndroidRuntime(835):java.lang.RuntimeException:无法启动活动ComponentInfo {io.raindance.kalimat/io.raindance.kalimat。 test.KalimatActivity}:java.lang.IllegalArgumentException:没有找到id为0x7f050005的片段CellFragment {4103c790#1 id = 0x7f050005}

我一直在寻找这个问题的原因和解决方案,现在两天了。

+0

尝试一个干净的项目 – nhaarman 2012-07-08 00:52:19

回答

0

需要创建Activity(并保存其状态),以便执行FragmentTransaction,否则您将收到异常。请尝试在onActivityCreated之内执行您的交易。

+0

它没有工作,我仍然得到相同的异常 – 2012-07-08 09:37:40

+0

您的代码也尝试在片段中嵌入片段。你不应该去实例化一个新的cellfragment,并尝试将其添加到父视图。这可能是你为什么会得到一个异常......你不能在片段中存在片段。你应该考虑阅读开发者网站上的片段。 – 2012-07-08 11:44:08

+0

我想这是它,你不能在其他片段内设置片段,谢谢 – 2012-07-09 08:42:55