【发布时间】:2011-11-15 07:27:54
【问题描述】:
我正在开发一个 android 应用程序,我在其中动态地制作表格行,然后动态地向它添加 imageviews 和 textviews。 它添加第一行,然后给出以下异常
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我正在使用以下代码:
ar1= new JSONArray(children.toString());
for(int mn=0;mn<ar1.length();mn++){
value=ar1.getString(mn);
TableRow tr = new TableRow(HomePageWithPhoneIsOfflineDialog.this);
tr.setBackgroundColor(Color.WHITE);
tr.setId(100+mn);
tr.setMinimumHeight(60);
tr.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView child= new TextView(HomePageWithPhoneIsOfflineDialog.this);
child.setId(200+mn);
child.setHeight(50);
child.setGravity(Gravity.CENTER);
child.setTextColor(Color.BLACK);
System.out.println(value);
child.setText(value);
System.out.println("adding iv");
tr.addView(iv,0);
System.out.println("adding child");
tr.addView(child);
System.out.println("adding table row");
table.addView(tr, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
}
谁能告诉我如何解决这个问题。
谢谢
【问题讨论】:
-
在哪里抛出异常?
-
在此处指定位置... tr.addView(child);并不总是一样。
标签: android tablelayout