java.lang.IllegalStateException:指定的子项已具有父项。您必须先调用子视图的父级removeView()
问题描述:
我在屏幕上显示了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>
答
在你for
声明中使用的是相同的TextView
和TableRow
对象每次你做的时间:
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如果你想要做更多的事情添加标识。