1
我已經爲activity_main.xml中的單選按鈕定義了xml。在主要活動中,我正嘗試向電臺組動態添加單選按鈕。 面臨這樣的錯誤如何將定義的單選按鈕佈局分配給動態單選按鈕
指定的孩子已經有一個父。你必須對孩子的父母先調用removeView()
RadioGroup answerOptions = (RadioGroup) findViewById(R.id.answers_options);
for (int i = 0; i < answersList.size() ; i++) {
RadioButton radioButton = new RadioButton(MainActivity.this);
radioButton = (RadioButton) findViewById(R.id.simpleRadioButton);
radioButton.setText(answersList.get(i).getOption_description());
answerOptions.addView(radioButton,i);
}
main_activity.xml
<RadioGroup
android:id="@+id/answers_options"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="1">
<RadioButton
android:textSize="22sp"
android:id="@+id/simpleRadioButton"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="0.20"
android:text=""
android:layout_gravity="left" />
</RadioGroup>
請提供一個完整的例子。尤其是,您應該有一個完整的Activity或Fragment類,其中包含您所詢問的代碼(很可能在onCreate()或onCreateView())中。您還應該提供完整的XML佈局文件。這意味着你應該有一個有效的根元素,比如LinearLayout或者RelativeLayout。注意**這並不意味着發佈你的所有代碼**。您應該只發布與問題相關的內容。 –