Java错误:指定的子项已经有父项。您必须首先在子级的父级上调用removeView()

发布于 2024-12-11 16:05:24 字数 8931 浏览 0 评论 0原文

我正在制作一个 Android 应用程序,其中我将动态行添加到表格布局中,但它只添加了 5 行,然后给出了异常:

java.lang.IllegalStateException:指定的子级已经有父级。
您必须首先在子级的父级上调用removeView()。

我正在使用以下代码:

                 if(profileimage.equals("")){}
                 else{
            System.out.println("image"+profileimage);
             TableRow tr = new TableRow(this);
             TextView profileimage1= new TextView(this);
             profileimage1.setTextColor(Color.BLACK);
             profileimage1.setText("Profile Image :");
              String imageUrl = "http://ondamove.it/English/images/users/";
                imageUrl=imageUrl+profileimage;
                System.out.println(imageUrl);
                ImageView image= new ImageView(this);
                image.setImageBitmap(DownloadFile(imageUrl));
                System.out.println(image.toString());
                image.setPadding(20, 10, 0, 0);
                tr.addView(profileimage1, lp);
                tr.addView(image);

                table.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

                View v= new View(this);
                v.setBackgroundColor(drawable.blue);
                v.setMinimumHeight(2);

                table.addView(v, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

        }
     if(firstname.equals(""))
     {}
     else
   {    
         System.out.println("firstname  in esleeeee"+firstname);
        TableRow tr = new TableRow(this);
        TextView firstnametext= new TextView(this);
        firstnametext.setText("First Name    :" + firstname);
        firstnametext.setHeight(50);
        firstnametext.setTextColor(Color.BLACK);
        //firstnametext.setGravity(Gravity.LEFT);
        //firstnametext.setPadding(10, 18,0,0);
        firstnametext.setLayoutParams(lp);
        tr.addView(firstnametext, lp);
        tr.setMinimumHeight(50);
         System.out.println("firstname"+firstname);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
   }
    System.out.println("bbbbbbb");

    if(lastname.equals(""))
            {}
    else


    {   
         System.out.println("lastname in elseeeee"+lastname);
        TableRow tr = new TableRow(this);
        TextView lastnametext= new TextView(this);
        lastnametext.setText("Last Name    :" + lastname);
        lastnametext.setHeight(50);
        lastnametext.setTextColor(Color.BLACK);
        lastnametext.setGravity(Gravity.LEFT);
        lastnametext.setPadding(10,18,0,0);

        lastnametext.setLayoutParams(lp);
        tr.addView(lastnametext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }


    if(birthday.equals("")){}
    else
    {   
        TableRow tr = new TableRow(this);
        TextView birtdaytext= new TextView(this);
        birtdaytext.setText("Birthday    :" + birthday);
        birtdaytext.setHeight(50);
        birtdaytext.setTextColor(Color.BLACK);
        birtdaytext.setGravity(Gravity.LEFT);
        birtdaytext.setPadding(10, 18,0,0);
        birtdaytext.setLayoutParams(lp);
        tr.addView(birtdaytext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

    }
    if(town.equals("")){}
    else
    {   
        TableRow tr = new TableRow(this);
        TextView towntext= new TextView(this);
        towntext.setText("Town    :" + town);
        towntext.setHeight(50);
        towntext.setTextColor(Color.BLACK);

        towntext.setGravity(Gravity.LEFT);
        towntext.setPadding(10, 18,0,0);

        towntext.setLayoutParams(lp);
        tr.addView(towntext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

    }


    if(email.equals("")){}
    else
    {   TableRow tr = new TableRow(this);
        TextView emailtext= new TextView(this);
        emailtext.setText("Email    :" + email);
        emailtext.setHeight(50);
        emailtext.setTextColor(Color.BLACK);
        emailtext.setGravity(Gravity.LEFT);
        emailtext.setPadding(10, 18,0,0);

        table.addView(emailtext);

        emailtext.setLayoutParams(lp);
        tr.addView(emailtext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        View v1= new View(this);
        v1.setBackgroundColor(drawable.blue);
        v1.setMinimumHeight(2);

        table.addView(v1, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        table.removeViewInLayout(tr);
        table.removeAllViews();

    }

    if(sex.equals("")){}
    else

    {
        System.out.println("in sex part"+sex);
        TableRow tr1 = new TableRow(this);
        TextView sextext= new TextView(this);
        sextext.setText("Sex    :" + sex);
        sextext.setHeight(50);
        sextext.setTextColor(Color.BLACK);
        sextext.setGravity(Gravity.LEFT);
        sextext.setPadding(10, 18,0,0);
        table.addView(sextext);
        sextext.setLayoutParams(lp);
        tr1.addView(sextext, lp);
        tr1.setMinimumHeight(50);
        table.addView(tr1, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v2= new View(this);
        v2.setBackgroundColor(drawable.blue);
        v2.setMinimumHeight(2);

        table.addView(v2, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));


    }

    if(cellno.equals("")){}
    else
    {   TableRow tr = new TableRow(this);
        TextView cellnotext= new TextView(this);
        cellnotext.setText("Cell No     :" + cellno);
        cellnotext.setHeight(50);
        cellnotext.setTextColor(Color.BLACK);
        cellnotext.setGravity(Gravity.LEFT);
        cellnotext.setPadding(10, 18,0,0);
        cellnotext.setLayoutParams(lp);
        tr.addView(cellnotext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }


    if(education.equals("")){}
    else{   
        TableRow tr = new TableRow(this);
        TextView edutext= new TextView(this);
        edutext.setText("Education     :" + education);
        edutext.setHeight(50);
        edutext.setTextColor(Color.BLACK);
        edutext.setGravity(Gravity.LEFT);
        edutext.setPadding(10, 18,0,0);
        edutext.setLayoutParams(lp);
        tr.addView(edutext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));;
    }

而且图像也不会被下载和显示。但下载图像的相同方法也适用于其他类。 有人能帮我解决这个问题吗?

I am making an android app in which i am adding dynamic rows to a table layout but it adds just 5 rows and then it gives the 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:

                 if(profileimage.equals("")){}
                 else{
            System.out.println("image"+profileimage);
             TableRow tr = new TableRow(this);
             TextView profileimage1= new TextView(this);
             profileimage1.setTextColor(Color.BLACK);
             profileimage1.setText("Profile Image :");
              String imageUrl = "http://ondamove.it/English/images/users/";
                imageUrl=imageUrl+profileimage;
                System.out.println(imageUrl);
                ImageView image= new ImageView(this);
                image.setImageBitmap(DownloadFile(imageUrl));
                System.out.println(image.toString());
                image.setPadding(20, 10, 0, 0);
                tr.addView(profileimage1, lp);
                tr.addView(image);

                table.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

                View v= new View(this);
                v.setBackgroundColor(drawable.blue);
                v.setMinimumHeight(2);

                table.addView(v, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));

        }
     if(firstname.equals(""))
     {}
     else
   {    
         System.out.println("firstname  in esleeeee"+firstname);
        TableRow tr = new TableRow(this);
        TextView firstnametext= new TextView(this);
        firstnametext.setText("First Name    :" + firstname);
        firstnametext.setHeight(50);
        firstnametext.setTextColor(Color.BLACK);
        //firstnametext.setGravity(Gravity.LEFT);
        //firstnametext.setPadding(10, 18,0,0);
        firstnametext.setLayoutParams(lp);
        tr.addView(firstnametext, lp);
        tr.setMinimumHeight(50);
         System.out.println("firstname"+firstname);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
   }
    System.out.println("bbbbbbb");

    if(lastname.equals(""))
            {}
    else


    {   
         System.out.println("lastname in elseeeee"+lastname);
        TableRow tr = new TableRow(this);
        TextView lastnametext= new TextView(this);
        lastnametext.setText("Last Name    :" + lastname);
        lastnametext.setHeight(50);
        lastnametext.setTextColor(Color.BLACK);
        lastnametext.setGravity(Gravity.LEFT);
        lastnametext.setPadding(10,18,0,0);

        lastnametext.setLayoutParams(lp);
        tr.addView(lastnametext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }


    if(birthday.equals("")){}
    else
    {   
        TableRow tr = new TableRow(this);
        TextView birtdaytext= new TextView(this);
        birtdaytext.setText("Birthday    :" + birthday);
        birtdaytext.setHeight(50);
        birtdaytext.setTextColor(Color.BLACK);
        birtdaytext.setGravity(Gravity.LEFT);
        birtdaytext.setPadding(10, 18,0,0);
        birtdaytext.setLayoutParams(lp);
        tr.addView(birtdaytext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

    }
    if(town.equals("")){}
    else
    {   
        TableRow tr = new TableRow(this);
        TextView towntext= new TextView(this);
        towntext.setText("Town    :" + town);
        towntext.setHeight(50);
        towntext.setTextColor(Color.BLACK);

        towntext.setGravity(Gravity.LEFT);
        towntext.setPadding(10, 18,0,0);

        towntext.setLayoutParams(lp);
        tr.addView(towntext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

    }


    if(email.equals("")){}
    else
    {   TableRow tr = new TableRow(this);
        TextView emailtext= new TextView(this);
        emailtext.setText("Email    :" + email);
        emailtext.setHeight(50);
        emailtext.setTextColor(Color.BLACK);
        emailtext.setGravity(Gravity.LEFT);
        emailtext.setPadding(10, 18,0,0);

        table.addView(emailtext);

        emailtext.setLayoutParams(lp);
        tr.addView(emailtext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        View v1= new View(this);
        v1.setBackgroundColor(drawable.blue);
        v1.setMinimumHeight(2);

        table.addView(v1, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        table.removeViewInLayout(tr);
        table.removeAllViews();

    }

    if(sex.equals("")){}
    else

    {
        System.out.println("in sex part"+sex);
        TableRow tr1 = new TableRow(this);
        TextView sextext= new TextView(this);
        sextext.setText("Sex    :" + sex);
        sextext.setHeight(50);
        sextext.setTextColor(Color.BLACK);
        sextext.setGravity(Gravity.LEFT);
        sextext.setPadding(10, 18,0,0);
        table.addView(sextext);
        sextext.setLayoutParams(lp);
        tr1.addView(sextext, lp);
        tr1.setMinimumHeight(50);
        table.addView(tr1, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v2= new View(this);
        v2.setBackgroundColor(drawable.blue);
        v2.setMinimumHeight(2);

        table.addView(v2, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));


    }

    if(cellno.equals("")){}
    else
    {   TableRow tr = new TableRow(this);
        TextView cellnotext= new TextView(this);
        cellnotext.setText("Cell No     :" + cellno);
        cellnotext.setHeight(50);
        cellnotext.setTextColor(Color.BLACK);
        cellnotext.setGravity(Gravity.LEFT);
        cellnotext.setPadding(10, 18,0,0);
        cellnotext.setLayoutParams(lp);
        tr.addView(cellnotext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    }


    if(education.equals("")){}
    else{   
        TableRow tr = new TableRow(this);
        TextView edutext= new TextView(this);
        edutext.setText("Education     :" + education);
        edutext.setHeight(50);
        edutext.setTextColor(Color.BLACK);
        edutext.setGravity(Gravity.LEFT);
        edutext.setPadding(10, 18,0,0);
        edutext.setLayoutParams(lp);
        tr.addView(edutext, lp);
        tr.setMinimumHeight(50);
        table.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        View v= new View(this);
        v.setBackgroundColor(drawable.blue);
        v.setMinimumHeight(2);

        table.addView(v, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));;
    }

And also the image does not get downloaded and shown. but the same method for donloading images is working in other class.
Can anyone help me over this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆凄美了谁 2024-12-18 16:05:24

您是否尝试过为表行或要添加到表中的视图提供唯一 ID?尝试为这些表行和视图设置 ID。

Have you tried giving an Unique ID for the table rows or the views you are adding to the table ?. Try setId for those table rows and the views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文