Android TableLayout - "The specified child already has a parent. You must call removeView() on the child's parent first."
I am looking at creating two areas on one screen that is generated from dynamic tablerows/tablelayout
First tablelayout will have 1 tablerow with 6 textview elements.
Second tablelayout will have 2 tablerows with 6 buttons in each.
Each Button / textview is bought in from a xml file.
Issue I am having that the error stated in title is being thrown when I try to run these two tables. .it Errors on the following line
tablerow.addView(View1);
CODE
Layout1 = (TableLayout) findViewById(R.id.scoring_tableLayout1);
Layout2 = (TableLayout) findViewById(R.id.scoring_tableLayout2);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
text = new TextView[6];
TableRow tablerow1 = new TableRow(this);
for (int i = 0; i < 6; i++) {
View1 = (View) inflater.inflate(R.layout.layout1, null);
text[i] = (TextView) endValueView.findViewById(R.id.textView_item);
tablerow.addView(View1);
}
Layout1.addView(tablerow1);
button = new Button[12];
for (int i = 0; i < 2; i++) {
TableRow tablerow2 = new TableRow(this);
for (int j = 0; j < 6; j++) {
ButtonView = (View) inflater.inflate(R.layout.button, null);
button[j] = (Button) ButtonView
.findViewById(R.id.scoring_arrow_score_button);
button[j].setText("some text");
开发者_开发问答 tablerow2.addView(ButtonView);
}
Layout2.addView(tablerow2);
}
Thanks for your Time
I think it is because you are calling the same textView_item within the loop.
text[i] = (TextView) endValueView.findViewById(R.id.textView_item);
How do you name your textView and ButtonView?
Ideally, what you can do is set the id of each textVeiw and ButtonView as tvItem1,tvItem2..etc.....
and please go to this link, in here you will find out how to do findViewById within the loop.
When you inflate a component you automatically add it to the view where the code is being executed. For example. If you have something like:
public calss MyClass extends LinearLayout {
...
private void init() {
inflater.inflate(R.layout.button, null);
...
you will be adding the visual component (button) to the LinearLayout (MyClass). That's why when you are looping and inflating the same button twice, the error says that the component has already been added.
What you really want to use is findViewById instead of the inflater you are using.
Good luck!
精彩评论