我正在制作一个 Android 应用程序,我在其中动态制作表格行,然后动态地向其添加 ImageView 和 TextView 。 它添加第一行然后给出以下异常
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));
谁能告诉我如何解决这个问题。
谢谢
最佳答案
您要添加的 iv 变量不是在循环内创建的,所以我想是在之前创建的......但在第二个循环中它被添加到两个不同的表行。
关于Android: java.lang.IllegalStateException: 指定的 child 已经有一个 parent 。您必须首先对 child 的 parent 调用 removeView()。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8132867/