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));;
    }

而且图像也没有被下载和显示。但是用于下载图像的相同方法正在其他类中工作。谁能帮我解决这个问题?

4

1 回答 1

0

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

于 2011-10-24T05:18:25.743 回答