2012-01-04 88 views
1

我在屏幕上顯示了2個單獨的表。 顯示第一個表格沒有問題。 參照TableLayout second_table,的TableRow tableRow5,文本字段(一個或多個)textView51,textView52,textView53java.lang.IllegalStateException:指定的子項已具有父項。您必須先調用子視圖的父級removeView()

的TextField基本列。要顯示三列 名字,姓氏,年齡。我需要從服務器獲取值後動態設置值。

我正面臨以下異常: java.lang.IllegalStateException:指定的子項已具有父項。您必須先調用子對象的父對象的removeView()。

我使用下面的代碼行填充第二個表:

public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.employee_details); 
TableLayout tableLay = (TableLayout) findViewById(R.id.second_table); 
TableRow tableRow = (TableRow) findViewById(R.id.tableRow5); 
TextView fNameTxtView = (TextView) findViewById(R.id.textView51); 
TextView lNameTxtView = (TextView) findViewById(R.id.textView52); 
TextView ageTxtView = (TextView) findViewById(R.id.textView53); 
for(int count = 0; count < tempArrList.size() ; count++){ 
fNameTxtView.setText(tempArrList.get(temp.get(0))); 
lNameTxtView.setText(tempArrList.get(temp.get(1))); 
ageTxtView.setText(tempArrList.get(temp.get(2))); 
    tableRow.addView(fNameTxtView); 
    tableRow.addView(lNameTxtView); 
    tableRow.addView(ageTxtView); 
    tableLay.addView(tableRow); // Add the new row to our tableLayout 
} 
} 

我曾嘗試一些變化過,但未能解決問題。 歡迎任何建議。

佈局XML如下: employee_details.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <include android:id="@+id/header" layout="@layout/activity_header" /> 
    <RelativeLayout android:id="@+id/RelativeLayout1" 
     android:background="@drawable/footerbg" android:layout_height="wrap_content" 
     android:layout_width="fill_parent" android:layout_alignParentBottom="true" 
     android:gravity="center"> 
     <Button android:layout_alignParentTop="true" 
      android:layout_height="50dp" android:layout_width="150dp" android:id="@+id/btn_done"></Button> 
    </RelativeLayout> 
    <ScrollView android:id="@+id/scrollView01" 
     android:layout_above="@id/RelativeLayout1" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" android:layout_below="@+id/header"> 
     <LinearLayout android:id="@+id/linear" 
      android:orientation="vertical" android:layout_gravity="center_horizontal" 
      android:layout_width="match_parent" android:layout_height="match_parent"> 
      <TextView android:id="@+id/hello_TxtView" 
       android:layout_width="wrap_content" android:layout_height="wrap_content"> 
      </TextView> 
      <TableLayout android:id="@+id/first_table" 
       android:layout_width="fill_parent" android:layout_height="wrap_content" 
       android:background="@drawable/table_shape"> 
       <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" 
        android:layout_height="wrap_content"> 
        <TextView android:id="@+id/textView11" 
         android:layout_width="fill_parent" android:layout_height="match_parent"> 
        </TextView> 
        <TextView android:id="@+id/textView12" 
         android:layout_width="fill_parent" android:layout_height="wrap_content"> 
        </TextView> 
       </TableRow> 
      </TableLayout> 
      <TableLayout android:id="@+id/second_table" 
       android:layout_width="match_parent" android:layout_height="match_parent"> 
       <TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" 
        android:layout_height="wrap_content"> 
        <TextView android:id="@+id/textView41" 
         android:layout_width="match_parent" android:layout_height="wrap_content"> 
        </TextView> 
        <TextView android:id="@+id/textView42" 
         android:layout_width="fill_parent" android:layout_height="wrap_content"> 
        </TextView> 
        <TextView android:id="@+id/textView43" 
         android:layout_width="fill_parent" android:layout_height="wrap_content"> 
        </TextView> 
       </TableRow> 
       <TableRow android:id="@+id/tableRow5" android:layout_width="match_parent" 
        android:layout_height="wrap_content" android:background="#f6f7fc"> 
        <TextView android:id="@+id/textView51" 
         android:layout_width="match_parent" android:layout_height="wrap_content"> 
        </TextView> 
        <TextView android:id="@+id/textView52" 
         android:layout_width="match_parent" android:layout_height="wrap_content"> 
        </TextView> 
        <TextView android:id="@+id/textView53" 
         android:layout_width="match_parent" android:layout_height="wrap_content"> 
        </TextView> 
       </TableRow> 
      </TableLayout> 
     </LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

回答

3

在你for聲明中使用的是相同的TextViewTableRow對象每次你做的時間:

tableRow.addView(fNameTxtView); 
    tableRow.addView(lNameTxtView); 
    tableRow.addView(ageTxtView); 
    tableLay.addView(tableRow); 
插入

在你的佈局中同樣是s fNameTxtView,lNameTxtView,ageTxtView。 爲了避免這種情況,你可以在你的動態創建對象的聲明是這樣的:

for(int count = 0; count < tempArrList.size() ; count++){ 
TextView fNameTxtView = new TextView(this); 
fNameTxtView.setText(tempArrList.get(temp.get(0))); 
TextView lNameTxtView= new TextView(this); 
lNameTxtView.setText(tempArrList.get(temp.get(1))); 
TextView ageTxtView = new TextView(this); 
ageTxtView.setText(tempArrList.get(temp.get(2))); 
TableRow tableRow = new TableRow(this); 
    tableRow.addView(fNameTxtView); 
    tableRow.addView(lNameTxtView); 
    tableRow.addView(ageTxtView); 
    tableLay.addView(tableRow); // Add the new row to our tableLayout 
} 

編輯:當然,你可以爲TextViews如果你想要做更多的事情添加標識。

相關問題