当前位置: 动力学知识库 > 问答 > 编程问答 >

android - java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first [for a Table]

问题描述:

I am drawing the table dynamically in my code. The tablerow I get from layout which I am just hardcoding in the XML. I don't know why I am getting this error and am completely out of ideas.

 TableRow tr = (TableRow) findViewById(R.id.tr);

TableRow.LayoutParams rowParams = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);

TextView textView1 = new TextView(this);

textView1.setGravity(Gravity.CENTER);

textView1.setBackgroundResource(R.drawable.cell_shape);

textView1.setText("1");

textView1.setLayoutParams(rowParams);

tr.addView(textView1);

TextView textView2 = new TextView(this);

textView2.setGravity(Gravity.CENTER);

textView2.setBackgroundResource(R.drawable.cell_shape);

textView2.setText("2");

textView2.setLayoutParams(rowParams);

tr.addView(textView2);

TextView textView3 = new TextView(this);

textView3.setGravity(Gravity.CENTER);

textView3.setBackgroundResource(R.drawable.cell_shape);

textView3.setText("3");

textView3.setLayoutParams(rowParams);

tr.addView(textView3);

TextView textView4 = new TextView(this);

textView4.setGravity(Gravity.CENTER);

textView4.setBackgroundResource(R.drawable.cell_shape);

textView4.setText("4");

textView4.setLayoutParams(rowParams);

tr.addView(textView4);

TextView textView5 = new TextView(this);

textView5.setGravity(Gravity.CENTER);

textView5.setBackgroundResource(R.drawable.cell_shape);

textView5.setText("5");

textView5.setLayoutParams(rowParams);

tr.addView(textView5);

TextView textView6 = new TextView(this);

textView6.setGravity(Gravity.CENTER);

textView6.setBackgroundResource(R.drawable.cell_shape);

textView6.setText("6");

textView6.setLayoutParams(rowParams);

tr.addView(textView6);

TextView textView7 = (TextView) findViewById(R.id.tv);

TableRow.LayoutParams params = (TableRow.LayoutParams)textView7.getLayoutParams();

params.span = 6;

textView7.setLayoutParams(params);

//tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

//next row

TableRow trNew = (TableRow) findViewById(R.id.newline);

TableRow.LayoutParams rowParams1 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);

TextView textView8 = (TextView) findViewById(R.id.model);

textView8.setGravity(Gravity.CENTER);

textView8.setBackgroundResource(R.drawable.cell_shape);

textView8.setText("Check");

trNew.addView(textView8);

The error is caused by: trNew.addView(textView8);

My error message is:

Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

网友答案:

Remove trNew.addView(textView8); if you want to use the TextView that you have created in XML. If you want to use the dynamically created TextView then replace

TextView textView8 = (TextView) findViewById(R.id.model);

by

 TextView textView8 = new TextView(this);
网友答案:

When you initialize textView8 using

TextView textView8 = (TextView) findViewById(R.id.model);

that means it is from the XML and probably already has a parent. That is why you are getting the IllegalStateException

Change it to

TextView textView8 = new TextView(this);

instead as you have done for all the other TextView

分享给朋友:
您可能感兴趣的文章:
随机阅读: