无线电组检查的ID值错误-1

时间:2018-09-24 13:20:25

标签: android radio-button

我想从RadioButton访问RadioGroup

int selectedId = radioFeeGroup.getCheckedRadioButtonId();

radioFeeButton = (RadioButton) findViewById(selectedId);

我得到selectedId值-1。这就是为什么应用程序崩溃的原因。

2 个答案:

答案 0 :(得分:2)

请确保您从组中选择了至少一个单选按钮。

See the Document

您的代码正确,

int selectedId = radioFeeGroup.getCheckedRadioButtonId();

radioFeeButton = (RadioButton) findViewById(selectedId);
  
    

如果您已经选择了RadioButton,则清理项目并再次重建,如果您从xml正确访问RadioGroup,它应该可以工作。

  

答案 1 :(得分:0)

检查

 radioFeeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    int radioButtonID = radioFeeGroup.getCheckedRadioButtonId();
                    View radioButton = radioFeeGroup.findViewById(radioButtonID);
                    int position = group.indexOfChild(radioButton);
                }
            });

检查此示例

How to get the id of selected radio button in android?

How to get id of selected radio button in a radio group