android程序报The specified child already has a parent错误
我在使用TableLayout布局动态生成表格数据时,提示这个异常,求高手解决:The specified child already has a parent. You must call removeView下面是我的Activity的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stock_list);
bindView();
}
private void bindView() {
//调用xml布局文件中的TableLayout的id
TableLayout table = (TableLayout) findViewById(R.id.inventory_table);
//循环将字符加入表格中
for (Inventory inventory : invs) {
TableRow row = new TableRow(this);
ProId = new TextView(this);
ProId.setText("" + inventory.getPRODID());
ProId.setTextColor(R.color.BLACK);
ONHAND = new TextView(this);
ONHAND.setText("" + inventory.getONHAND());
COMMIT = new TextView(this);
COMMIT.setText("" + inventory.getCOMMIT());
HCOMMIT = new TextView(this);
HCOMMIT.setText("" + inventory.getHCOMMIT());
SCOMMIT = new TextView(this);
SCOMMIT.setText("" + inventory.getSCOMMIT());
ONRECEIPT = new TextView(this);
ONRECEIPT.setText("" + inventory.getONRECEIPT());
row.addView(ProId);
row.addView(ONHAND);
row.addView(COMMIT);
row.addView(HCOMMIT);
row.addView(SCOMMIT);
row.addView(ONRECEIPT);
table.addView(row, new TableLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}
stock_list.xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/WHITE_BG"
android:padding="10dp" >
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableLayout
android:id="@+id/inventory_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stretchColumns="0,1,2,3,4,5,6" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:gravity="left"
android:padding="2dp"
android:text="編號"
android:textColor="#000"
android:textSize="10pt"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:padding="2dp"
android:text="商品名稱"
android:textColor="#000"
android:textSize="10pt"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:padding="2dp"
android:text="ONHAND"
android:textColor="#000"
android:textSize="10pt"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:padding="2dp"
android:text="COMMIT"
android:textColor="#000"
android:textSize="10pt"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:padding="2dp"
android:text="HCOMMIT"
android:textColor="#000"
android:textSize="10pt"
android:textStyle="bold" />
<TextView
android:gravity="center"
android:padding="2dp"
android:text="SCOMMIT"
android:textColor="#000"
android:textSize="10pt"
android:textStyle="bold" />
<TextView
android:gravity="right"
android:padding="2dp"
android:text="ONRECEIPT"
android:textColor="#000"
android:textSize="10pt"
android:textStyle="bold" />
</TableRow>
<View
android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
</HorizontalScrollView>
</ScrollView>
--------------------编程问答-------------------- 我这没问题啊
我这手机是4.0的
你运行的环境是什么啊
补充:移动开发 , Android