我在使用 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 中也没有添加子单选按钮。我错过了什么/
提前致谢