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

완전한 예를 제공해주십시오. 특히, onCreate() 또는 onCreateView()에서 자주 묻는 코드가 포함 된 전체 Activity 또는 Fragment 클래스가 있어야합니다. 또한 완전한 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에 정의 된 동일한 속성을 설정하려면 프로그래밍을 수행하거나 새로운 레이아웃을 생성하고 팽창 할 수있다.