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,我只是在XML中硬编码。我不知道为什么我会收到这个错误,完全没有想法。
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);
该错误是由以下原因引起的: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.
2 个解决方案
#1
3
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
删除trNew.addView(textView8);如果要使用在XML中创建的TextView。如果要使用动态创建的TextView,则替换
TextView textView8 = (TextView) findViewById(R.id.model);
by
通过
TextView textView8 = new TextView(this);
#2
5
When you initialize textView8
using
使用时初始化textView8
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
这意味着它来自XML,可能已经有了父。这就是你得到IllegalStateException的原因
Change it to
将其更改为
TextView textView8 = new TextView(this);
instead as you have done for all the other TextView
而是你已经为所有其他TextView做了
#1
3
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
删除trNew.addView(textView8);如果要使用在XML中创建的TextView。如果要使用动态创建的TextView,则替换
TextView textView8 = (TextView) findViewById(R.id.model);
by
通过
TextView textView8 = new TextView(this);
#2
5
When you initialize textView8
using
使用时初始化textView8
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
这意味着它来自XML,可能已经有了父。这就是你得到IllegalStateException的原因
Change it to
将其更改为
TextView textView8 = new TextView(this);
instead as you have done for all the other TextView
而是你已经为所有其他TextView做了