安卓:java.lang.IllegalStateException:指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。 [英] Android: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
本文介绍了安卓:java.lang.IllegalStateException:指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我mking中,我想提出动态表行一个Android应用程序,然后动态地添加imageviews和textviews它。
它增加了第一排,然后给下面的异常
I am mking an android app in which i am making dynamically table rows and then adding imageviews and textviews to it dynamically. It adds the first row and then give the following exception
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
我用下面的code吧:
I am using the following code for it:
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));
}
谁能告诉我怎么解决这个问题。
Can anyone tell me how to solve this problem.
感谢
推荐答案
时,没有循环,所以我想象的是之前创建中创建的四变量要添加,......但在第二循环中它添加两个不同的表行。
The iv variable that you are adding, is not been created inside the loop so I imagine is created before... but in the second loop it's added to two different Table Rows.
这篇关于安卓:java.lang.IllegalStateException:指定的小孩已经有一个父。您必须先对孩子的父母打电话removeView()。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文