【发布时间】:2018-12-29 06:32:46
【问题描述】:
我正在尝试在片段而不是活动中使用 RadioGroup 小部件。以下代码行出现错误:
RadioButton button = new RadioButton(this);
我不确定要传递什么而不是“this”
我相信问题出在上面的 sn-p 或这里:
RadioGroup group = (RadioGroup) getView().findViewById(R.id.radioGroup);
完整代码:
public class fragment1 extends Fragment {
private Button btnNextFrag;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
createRadioButtons();
return view;
}
private void createRadioButtons() {
RadioGroup group = (RadioGroup) getView().findViewById(R.id.radioGroup);
String[] questions1 = getResources().getStringArray(R.array.question_1_answers);
for (int i=0;i<questions1.length;i++){
String question = questions1[i];
RadioButton button = new RadioButton(this);
button.setText(question);
group.addView(button);
}
}
}
【问题讨论】:
标签: android-studio android-fragments radio-group