mango
mango

Reputation: 5636

how to clear radio group and use oncheckchanged listener at the same time?

My dilemna is that I want to use

radioGrp.clearCheck(); 

After some research, i found that the way to do it is to it like

        selectedid = radioGrp.getCheckedRadioButtonId();
    if (selectedid > 0) {
        radioGrp.clearCheck();
    }

this is because of some issue where the getCheckid might come up a negative number or something of the sort. However, how can i use that and the following in the same activity. The oncheckedchangedlistener has it's own int id that i believe is causing issue....

radioGrp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
.....

i regularly get this error

10-04 08:44:03.728: E/AndroidRuntime(420): FATAL EXCEPTION: main
10-04 08:44:03.728: E/AndroidRuntime(420): java.lang.NullPointerException
10-04 08:44:03.728: E/AndroidRuntime(420):  at com.mangodeveloper.mcathomie.McatActivityGame$1.onCheckedChanged(McatActivityGame.java:93)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.widget.RadioGroup.setCheckedId(RadioGroup.java:172)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.widget.RadioGroup.check(RadioGroup.java:166)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.widget.RadioGroup.clearCheck(RadioGroup.java:205)
10-04 08:44:03.728: E/AndroidRuntime(420):  at com.mangodeveloper.mcathomie.McatActivityGame.fillQuestions(McatActivityGame.java:83)
10-04 08:44:03.728: E/AndroidRuntime(420):  at com.mangodeveloper.mcathomie.McatActivityGame.access$8(McatActivityGame.java:51)
10-04 08:44:03.728: E/AndroidRuntime(420):  at com.mangodeveloper.mcathomie.McatActivityGame$2.onClick(McatActivityGame.java:128)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.view.View.performClick(View.java:2485)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.view.View$PerformClick.run(View.java:9080)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.os.Handler.handleCallback(Handler.java:587)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.os.Handler.dispatchMessage(Handler.java:92)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.os.Looper.loop(Looper.java:123)
10-04 08:44:03.728: E/AndroidRuntime(420):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-04 08:44:03.728: E/AndroidRuntime(420):  at java.lang.reflect.Method.invokeNative(Native Method)
10-04 08:44:03.728: E/AndroidRuntime(420):  at java.lang.reflect.Method.invoke(Method.java:507)
10-04 08:44:03.728: E/AndroidRuntime(420):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-04 08:44:03.728: E/AndroidRuntime(420):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-04 08:44:03.728: E/AndroidRuntime(420):  at dalvik.system.NativeStart.main(Native Method)

Upvotes: 2

Views: 8190

Answers (1)

mango
mango

Reputation: 5636

Alright well i was able to figure out a work-around, elementary as it is - i'm just embarrassed that i didn't think of it sooner. Basically I just call the clear radiogroup command BEFORE i set the check changed listeners. But if your activity has these in a loop like mine did, then you have to set the setOnCheckedChangeListener to null before the the clear command comes on again. Here is my skeleton of the solution with the important parts. It should work fine 'till something better comes along.

private void Method1() {    

    ...

    selectedid = radioGrp.getCheckedRadioButtonId();
    if (selectedid > 0) {
        radioGrp.clearCheck();
    }
    // ^ must come before the following method.             
    activateButtons();
}


private void activateButtons() {

    radioGrp.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            ...

            radioGrp.setOnCheckedChangeListener(null);
        }
    });
    Method1();
}

Upvotes: 2

Related Questions