java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()

时间:2012-01-04 15:43:20

标签: android

我在屏幕上显示2个单独的表格。 显示第一个表没有问题。 参考TableLayout second_table ,TableRow tableRow5 ,Textfield textView51,textView52,textView53

TextFields基本上就是列。要显示的三列 名字,姓氏,年龄。我需要在从服务器获取值后动态设置值。

我面临以下异常: 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>

1 个答案:

答案 0 :(得分:3)

for语句中,您每次都使用相同的TextViewTableRow个对象:

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

在布局中插入相同的TextView的fNameTxtView,lNameTxtView,ageTxtView。 为避免这种情况,您可以在for语句中动态创建对象,如下所示:

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添加ID。