Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5992079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:28:47+00:00 2026-05-22T23:28:47+00:00

I edited the code the way @CommonsWare told me i get force close when

  • 0

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

  • count the “yes” answers yes++

  • If the user clicks the second time don’t count

  • If the user changes their mind from “yes” to “no” decrement “yes”

  • If the user presses “no” Don’t count anything (the important part of my application is to count the yes answers )

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T23:28:48+00:00Added an answer on May 22, 2026 at 11:28 pm

    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() {

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have HTML code edited by FCKEditor stored in a database and would like
Problem: edited files on windows, using git-bash, to fix IE7 problems committed, pushed to
This is a sample (edited slightly, but you get the idea) of my XML
I wrote a visual c# 2008 windows form application ,then i edited the code
Example code : EDITED : clarified example. Sorry for any confusion. using System.Collections.Specialized; using
I edited the question after David Hanak's answer (thanks btw!). He helped with the
How do I load the edited .emacs file without restarting Emacs?
I have a JavaScript resource that has the possibility of being edited at any
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
My VB.Net Winforms app is a tool to allow hierarchical data to be edited

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.