gpt4 book ai didi

安卓 : Error when adding radio group programmatically

转载 作者:行者123 更新时间:2023-11-30 04:05:03 26 4
gpt4 key购买 nike

我有一个错误,我不明白它是什么意思。我是 Android 的新手

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

我正在尝试以编程方式将单选组添加到表格布局

首先我将单选组添加到表格布局,然后将单选按钮添加到单选组

RadioGroup mRadioGroup;
mRadioGroup = new RadioGroup(this);
TableLayout mainTable = (TableLayout) findViewById(R.id.myTable);
mainTable.addView(mRadioGroup);

然后我创建一行并将单选组添加到这一行,最后将该行添加到表格布局

TableRow row;
RadioButton radioButton = new RadioButton(this);
radioButton.setId(1);
radioButton.setText("SomeText");
row.addView(mRadioGroup);

mainTable.addView(row);

有人能帮忙吗?

编辑:当我直接在表格布局而不是表格行上添加 radio 组时,它起作用了

最佳答案

您的问题是您将 mRadioGroup 添加到 mainTablerow:

mainTable.addView(mRadioGroup);
...
row.addView(mRadioGroup);

正如您部分发现的那样,删除其中任何一行,它将起作用。

不必View 添加到 TableRow 即可将其添加到 TableLayout,但它的行为会有所不同,具体取决于您是否这样做。

此外,您从未真正初始化过 row - 确保这样做

TableRow row = new TableRow(this);

在你使用它之前!

关于安卓 : Error when adding radio group programmatically,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11815819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com