0

I'm currently making an app with a lot of checkboxes (39 to be spcific) and I want to implement a button that will uncheck them when it's clicked. I haven't done much with buttons (I only used them to start a new activity so far) so I'm still not completely sure how they work. After doing some research this is what I came up with. Even though I have 39 checkboxes I will only paste a few in this code to keep it short.

CheckBox cb1 = (CheckBox) findViewById(R.id.checkBox2);
CheckBox cb2 = (CheckBox) findViewById(R.id.checkBox3);
CheckBox cb3 = (CheckBox) findViewById(R.id.checkBox4);
public void onClick (View v){
    switch (v.getId()) {
        case R.id.button1:
            cb1.setChecked(false);
            cb2.setChecked(false);
            cb3.setChecked(false);
    }
}

LogCat:

06-12 06:45:09.922: E/AndroidRuntime(6623): FATAL EXCEPTION: main 
06-12 06:45:09.922: E/AndroidRuntime(6623): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.gw2legendary/com.example.gw2legendary.Bifrost}: java.lang.NullPointerException 
06-12 06:45:09.922: E/AndroidRuntime(6623): Caused by: java.lang.NullPointerException

The problem is that my activity crashes as soon as I try to open it with I add this code to it. The activity starts fine if I remove this code from it.

Guy
  • 6,414
  • 19
  • 66
  • 136
  • where do you have all the 39 check boxes in a listview? – Raghunandan Jun 12 '13 at 04:43
  • 06-12 06:45:09.922: E/AndroidRuntime(6623): FATAL EXCEPTION: main 06-12 06:45:09.922: E/AndroidRuntime(6623): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.gw2legendary/com.example.gw2legendary.Bifrost}: java.lang.NullPointerException 06-12 06:45:09.922: E/AndroidRuntime(6623): Caused by: java.lang.NullPointerException – Guy Jun 12 '13 at 04:46
  • It actually crashes when I try to open the activity that contains this button. – Guy Jun 12 '13 at 04:47
  • Please post whole logcat in question. You can edit it. – MysticMagicϡ Jun 12 '13 at 04:47
  • Then post the code when you start the new activity. Because that is where the problem occurs – MysticMagicϡ Jun 12 '13 at 04:48
  • I have them in a Scroll View. The activity started completely fine until I added this code for unchecking boxes so the problem must be here. – Guy Jun 12 '13 at 04:49
  • Is this activity implementing onClickListener? – MysticMagicϡ Jun 12 '13 at 04:54
  • No, but it is implementing an OnCheckedChangeListener – Guy Jun 12 '13 at 04:56
  • Implement onClickListener. Otherwise don't write onClick the way you have written. – MysticMagicϡ Jun 12 '13 at 04:57
  • @AReader: I'm pretty sure he declared the onClick on the XML – dmon Jun 12 '13 at 04:58
  • @dmon I don't know, I am not sure he did so. The method name is onClick. And he's checking using switch case about checked button's id too. He might have to check that once, I think. – MysticMagicϡ Jun 12 '13 at 04:59
  • I did implement the onClick listener now and did what "dmon" told me to do and it works. Thanks for your help! – Guy Jun 12 '13 at 05:01
  • Glad we could help. Happy coding :) – MysticMagicϡ Jun 12 '13 at 05:02

1 Answers1

1

So the reason your code is failing: you declared your CheckBox variables as class fields, and they are initialized when the Activity starts, so the findViewById() returns null for all of them.

A quick fix: move all of those Checkbox declarations inside of your onClick method. Though it seems you might be using them somewhere else before the click.

A better potential solution: instead of trying to reference all of your 39 (!) checkboxes, it might be better to just get a reference to the container and iterate through all of the children, check if they are checkboxes and uncheck them if they are.

I will say that 39 checkboxes seems a bit ridiculous though.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • I moved those declarations inside the method and it worked perfectly! Thank you. Could you just please give me a small explanation in the "containter" and "children"? I'm new to android programming so those expressions are new to me. – Guy Jun 12 '13 at 05:00
  • It's hard to help you without your XML (so I know what the container is). The "container" is just the parent ViewGroup (e.g a LinearLayout) that has all of these checkboxes. You can iterate through a ViewGroup's children (using the [getChildAt and getChildCount](http://developer.android.com/reference/android/view/ViewGroup.html#getChildAt(int)) methods. Then you can use `instanceof` to make sure that the `childAt(x)` is actually a Checkbox. – dmon Jun 12 '13 at 05:02
  • Thanks, I will do some more research on this when I get home from school :) But I can tell right now that thoes checkboxer are inside Relative Layout which is inside Scroll View. – Guy Jun 12 '13 at 05:09