reiley
reiley

Reputation: 3761

RadioGroup.check(int) not working

I'm having a little trouble with RadioGroups. I created a blank radiogroup in layout. And then filled it with equalizer presets in the code. Then I tried to set one of the option using radiogroup.check(1). But it threw a null pointer exception.

Below is the code:

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);
}

It seems like even after adding the child radio buttons fillRadioGroupWithRadioButtons(), there are no childs added in radiogroup. What am I missing/

Thanks in advance

Upvotes: 1

Views: 2403

Answers (2)

reiley
reiley

Reputation: 3761

Done with a work around I guess

        RadioButton o = (RadioButton) rgEqualizer.getChildAt(1);
        o.setChecked(true);

Upvotes: 4

Vipul
Vipul

Reputation: 28093

You have defined rgEqualizer two times in your code.

    rgEqualizer = (RadioGroup) findViewById(R.id.rgEqualizerPreset);   <----- Here
    mPlayer = new MediaPlayer();
    mEqualizer = new Equalizer(0, AuID);
    rgEqualizer = new RadioGroup(this);   <---- And here //Delete this

Upvotes: 0

Related Questions