I am having an error that I don’t understand what does it mean. I am kindly new to Android
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I am trying to add a radio group to a table layout programmatically
first I add the radio group to the table layout and i add the radio button to the radio group
RadioGroup mRadioGroup;
mRadioGroup = new RadioGroup(this);
TableLayout mainTable = (TableLayout) findViewById(R.id.myTable);
mainTable.addView(mRadioGroup);
Then I create a row and add the radio group to this row and finally ad the row to the table layout
TableRow row;
RadioButton radioButton = new RadioButton(this);
radioButton.setId(1);
radioButton.setText("SomeText");
row.addView(mRadioGroup);
mainTable.addView(row);
Can any one help ?
EDIT : It worked when i added the radio group directly on the table layout not on a table row
Your problem was that you were adding
mRadioGroupto bothmainTableandrow:As you partly discovered, remove either one of these lines and it will work.
You don’t have to add a
Viewto aTableRowin order to add it to aTableLayout, but it behaves differently depending on whether or not you do.Also, you never actually initialized
row– make sure to dobefore you use it!