android程序报The specified child already has a parent错误

猴子搬来的救兵Castiel 2012-04-17 09:31:10
我在使用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>

...全文
201 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yitiaochongzi 2012-04-17
  • 打赏
  • 举报
回复
我这没问题啊
我这手机是4.0的
你运行的环境是什么啊

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧