Android: java.lang.IllegalStateException: 指定的 child 已经有一个 parent 。您必须首先对 child 的 parent 调用 removeView()。

标签 android tablelayout

我正在制作一个 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/

相关文章:

android - RenderScript Intrinsics 高斯模糊

android - 访问 TableLayout 中的内容

android - 为 BottomNavigationView 上的选定图标设置动画

android - 你如何创建一个只允许水平方向的应用程序?

android - 如何在 Android 的 tableLayout 中动态添加一行

html - 如何固定 table 的高度?

android - 如何在 ListView 中实现表格布局

android - 如何改变聚焦 View 的背景颜色

android - android 中的 opengl 表面应用程序示例

android - 提取通知待定 Intent 内容以实现辅助功能