0

我在使用 RadioGroups 时遇到了一些麻烦。我在布局中创建了一个空白无线电组。然后用代码中的均衡器预设填充它。然后我尝试使用 radiogroup.check(1) 设置选项之一。但是它抛出了一个空指针异常。

下面是代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.equalizer);
    //rgEqualizer = (RadioGroup) findViewById(R.id.rgEqualizerPreset);
    mPlayer = new MediaPlayer();
    mEqualizer = new Equalizer(0, AuID);
    rgEqualizer = new RadioGroup(this);
    radioButtonList = new ArrayList<RadioButton>();
    layoutParams = new RadioGroup.LayoutParams(
        RadioGroup.LayoutParams.FILL_PARENT,
        RadioGroup.LayoutParams.WRAP_CONTENT);

    fillRadioGroupWithRadioButtons();
    rgEqualizer.setEnabled(true);
    rgEqualizer.setOnCheckedChangeListener(this);
    addContentView(rgEqualizer, layoutParams);
    loadPresetsettings();
}
private void fillRadioGroupWithRadioButtons() {
    Short noPresets = mEqualizer.getNumberOfPresets();
    short i = 0;
    while (i < noPresets) {
        RadioButton rb = new RadioButton(this);
        rb.setText(mEqualizer.getPresetName(i));
        rgEqualizer.addView(rb, layoutParams);
        i++;
    }
}
private void loadPresetsettings() { 
    rgEqualizer.check(1);
}

似乎即使在添加子单选按钮 fillRadioGroupWithRadioButtons() 之后,radiogroup 中也没有添加子单选按钮。我错过了什么/

提前致谢

4

2 回答 2

4

完成了我想的工作

        RadioButton o = (RadioButton) rgEqualizer.getChildAt(1);
        o.setChecked(true);
于 2012-06-17T09:58:43.767 回答
0

您在代码中定义了 rgEqualizer 两次。

    rgEqualizer = (RadioGroup) findViewById(R.id.rgEqualizerPreset);   <----- Here
    mPlayer = new MediaPlayer();
    mEqualizer = new Equalizer(0, AuID);
    rgEqualizer = new RadioGroup(this);   <---- And here //Delete this
于 2012-06-17T07:52:15.193 回答