異常

2012-07-08 88 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