Android: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.
我正在使用以下代码:
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));
}
任何人都可以告诉我如何解决这个问题。
谢谢
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.
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.
THAnks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您添加的 iv 变量不是在循环内创建的,所以我想是之前创建的......但在第二个循环中,它被添加到两个不同的表行中。
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.
我认为问题在于
ImageView
(iv),因为它没有在循环内实例化,所以第一次将其添加到一行中,当您再次将其添加到另一行时,它不会不允许,因为一个视图只能有 1 个父视图。所以我建议你在每次循环时创建一个新的 ImageView 。
Problem is with the
ImageView
(iv), i think, since it is not instantiated inside the loop, so first time it was added to a row, and when you add it again to another row, it doesn't permit, because a view can have only 1 parent.so i would suggest you to create a new ImageView for every time you loop.
因为您在每次迭代中添加具有新值的相同视图。
在 tr.addView(iv,0) 之前初始化 imageView iv ;
because you are adding same View with new value in each iteration .
initialize imageView iv ,before tr.addView(iv,0);