Michiru
Michiru

Reputation: 31

check the state of a radio button android and count the answers

I edited the code the way @CommonsWare told me i get force close when I try to click r1 and the problem is it seems button[i].isChecked() doesn;t work--> fixed

I have final RadioButton[] buttons = {r1,r3,r5,r7};

And I want to count them (basically yes answers)

public void onClick(View view){


    checkStates(buttons);}
}
private void  checkStates(RadioButton[] buttons) { 
     for (int i = 0; i < 4; i++) {
        if (buttons[i].isChecked())

             yes++;}
     if (yes>=3){
      Intent intent=new Intent(this,AnotherClass.class);
        startActivity(intent);
  }

  }

Basically I have yes and no radio buttons. I want to count the yes answers and if the user changes their mind and checks no to decrement yes

r1 r3 r5 etc are "yes" answers r2 r4 r6 are "no" answers

My approach might be wrong since I'm new to this but if someone helps me I'll post the whole thing for people to see.

Please help!

Ok I checked the log cat and i receive the following error (it was obvious where but I don't know why and how can I implement this to work)

Android Runtime:  Uncaught handler :thread main exiting due to uncaught exception

java.lang.NullPointerException  
RadioButtons.checkStates(RadioButtons.java:49) --> here  : if (buttons[i].isChecked()) 
RadioButtons.onClick(RadioButtons.java:42)--> here:    checkStates(buttons);

Edit:The problem - the way @CommonsWare pointed out-was that buttons wasn;t initialized and I got NullPointerException So I changed the code before with this one:RadioButton[]buttons =new RadioButton[3]; gave the size of the array

 public void onClick(View view){
checkStates(buttons);
      }
private void  checkStates(RadioButton[] buttons) { 
        buttons[0]=rb1;
        buttons[1]=rb3;
        buttons[2]=rb5;

        if (buttons[i].isChecked()){

               yes++;}
           tv.setText("Result:"+yes);

    }

The problem this time is that It still counts the clicks and not the "yes" answers I have the following problems to resolve

Upvotes: 1

Views: 15095

Answers (2)

storm_to
storm_to

Reputation: 1575

First you need to use a RadioGroup to group each Yes and No pair so that Both are not allowed to be selected in the same time.

Here is a great starting resource http://developer.android.com/resources/tutorials/views/hello-formstuff.html#RadioButtons

Read this page and all your questions will be answered, including how to setup OnClickListener

Hope this helps

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006944

How to check the state of a radio button in android?

Call isChecked(). But you knew this already.

And I want to count them

There is no point in calling setChecked(true) on a RadioButton that returns true from isChecked(), since that RadioButton is already checked.

I want to count the yes answers and if the user changes their mind and checks no to decrement yes

Replace your // if r1 or r3 are checked{ with if buttons[i].isChecked() {

Upvotes: 1

Related Questions