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); 
    } 

あなたがXMLで定義されているのと同じのRadioButtonへの参照を取得しているためだ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> 
+0

完全な例を示してください。特に、あなたが質問しているコードを含む完全なActivityクラスまたはFragmentクラスを持つ必要があります(ほとんどの場合、onCreate()またはonCreateView())。また、完全なXMLレイアウトファイルを提供する必要があります。つまり、LinearLayoutやRelativeLayoutなどの有効なルート要素が必要です。注**これはあなたのコードをすべて投稿することを意味するものではありません**。それでも、質問に関連するものだけを投稿するべきです。 –

答えて

1

の一部。

for (int i = 0; i < answersList.size() ; i++) { 
    RadioButton radioButton = new RadioButton(MainActivity.this); 
    radioButton.setText(answersList.get(i).getOption_description()); 
    answerOptions.addView(radioButton,i); 
} 

をあなたはプログラムでそれを行うか、新しいレイアウトを作成し、それを膨らませることができ、XMLで定義された同じプロパティを設定するには:

はやってみてください。

関連する問題