我正在创建一个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));;
}
此外,图片无法下载和显示。但是,用于加载图像的相同方法在其他类中也有效。 谁能帮助我呢?
答案 0 :(得分:0)
您是否尝试为表行或要添加到表中的视图提供唯一ID?尝试使用setId表示这些表行和视图。