2014-04-25 14 views
0

我試圖將新的TableRows添加到TableLayout,但我甚至無法將TextView添加到新的TableRow對象。但總是有相同的錯誤:無法將TextView添加到新的TableRow中(指定的子項已有父項)

04-25 12:11:42.329: E/AndroidRuntime(26636): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

這是主代碼:

public static class PlaceholderFragment extends Fragment { 

protected TableLayout mTableLayout; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View rootView = inflater.inflate(R.layout.fragment_results, 
      container, false); 

    mTableLayout = (TableLayout) rootView 
      .findViewById(R.id.results_table); 
    fullFillTable(); 

    return rootView; 
} 

private void fullFillTable() { 
    TextView labelPosition = new TextView(getActivity()); 
    TableRow row = new TableRow(getActivity()); 
    labelPosition.setText("Pos"); 
    row.addView(labelPosition); //The error is here 
    mTableLayout.addView(row); 
    } 
} 

這裏是fragment.xml

<TableLayout 
     android:id="@+id/results_table" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="@dimen/activity_vertical_margin" 
     android:layout_marginLeft="@dimen/activity_horizontal_margin" 
     android:layout_marginRight="@dimen/activity_horizontal_margin" 
     android:layout_marginTop="@dimen/activity_vertical_margin" 
     android:stretchColumns="*" > 

     <TableRow 
      android:id="@+id/results_titles_row" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#F7F7F7" > 

      <TextView 
       android:id="@+id/results_col1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/results_col1_title" /> 

      <TextView 
       android:id="@+id/results_col2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/results_col2_title" /> 

      <TextView 
       android:id="@+id/results_col3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/results_col3_title" /> 

      <TextView 
       android:id="@+id/results_col4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/results_col4_title" /> 
     </TableRow> 
</TableLayout> 

有誰知道如何解決這個問題?

感謝

回答

0

你加入你的labelPosition以兩種不同的看法,這是錯誤的:

row.addView(labelPosition); 
mTableLayout.addView(labelPosition); 

相反,你應該添加TextViewTableRow並添加TablewRowTableLayout,就像這樣:

row.addView(labelPosition); 
mTableLayout.addView(row); 
+0

謝謝,我改變了這一點。但問題出在第一個addView() –

+0

@marc_aragones,你確定嗎?您是否使用固定代碼重建項目? –

+0

是的,當然。 logcat說錯誤在這行:row.addView(labelPosition); –

0

我已經能夠解決它,雖然不是我想要的方式。

創建一個row_layout.xml文件:

<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <TextView 
     android:id="@+id/col1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Hello world" /> 
</TableRow> 

然後,它充氣:

private void fullFillTable() { 
    TableRow row; 
    row = (TableRow) View.inflate(getActivity(), R.layout.row_layout, null); 
    ((TextView)row.findViewById(R.id.col1)).setText("Pos"); 
    mTableLayout.addView(row); 
} 

不過,我想知道爲什麼它不工作之前。

相關問題