• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Display different text in TextView based on what checkboxes are checked

Hello everyone! This part of code doesn't seem to be working, particularly the method isChecked(), because I have imported checkBox id as static import static todo.beginner.com.carchoose.R.id.checkBoxPrice;, but if checkBox is defined under class then the app is crushing when button is pressed to launch second activity. Also textView1 is in the SecondActivity, but check boxes are in MainAcitivy. Is there some way of making this part work and keeping checkbox imported as static? Here is a part from MainActivity.java:

Code:
initialUISetup();
   }
public void initialUISetup() {
   CheckBox checkBoxPrice = (CheckBox) findViewById(R.id.checkBoxPrice);
   CheckBox checkBoxGas = (CheckBox) findViewById(R.id.checkBoxGas);
   CheckBox checkBoxYear = (CheckBox) findViewById(R.id.checkBoxYear);
   CheckBox checkBoxMileage = (CheckBox) findViewById(R.id.checkBoxMileage);
   CheckBox checkBoxCapacity = (CheckBox) findViewById(R.id.checkBoxCapacity);

   checkBoxPrice.setOnCheckedChangeListener(new myCheckBoxChangeClicker());
   checkBoxGas.setOnCheckedChangeListener(new myCheckBoxChangeClicker());
   checkBoxYear.setOnCheckedChangeListener(new myCheckBoxChangeClicker());
   checkBoxMileage.setOnCheckedChangeListener(new myCheckBoxChangeClicker());
   checkBoxCapacity.setOnCheckedChangeListener(new myCheckBoxChangeClicker());
}

class myCheckBoxChangeClicker implements CheckBox.OnCheckedChangeListener

{

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {


       if(checkBoxPrice.isChecked() || checkBoxGas.isChecked() ||  checkBoxYear.isChecked() || checkBoxMileage.isChecked() || checkBoxCapacity.isChecked())
       {
           textView1.setText("Audi");
       }
   }
}
 
Can you define 'not working', and please show the stack trace from your Logcat if the application crashed.
 
Can you define 'not working', and please show the stack trace from your Logcat if the application crashed.
It is unable to start second activity.
I'll put this as code in hope that it is easier to look at it.
This is what logcat shows:
Code:
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: todo.raitis.com.carchooser, PID: 3047
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{todo.starter.com.phoneapp/todo.starter.com.phoneapp.SecondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference
                      at todo.raitis.com.carchooser.SecondActivity.initialUISetup(SecondActivity.java:177)
                      at todo.raitis.com.carchooser.SecondActivity.onCreate(SecondActivity.java:167)
                      at android.app.Activity.performCreate(Activity.java:6662)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
 
Your problem is a null pointer at line 177

Code:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)' on a null object reference at todo.raitis.com.carchooser.SecondActivity.initialUISetup(SecondActivity.java:177)

Where is line 177? If you post your entire class, not just the fragment, it would help with matching line numbers in the stack trace.

Also, please read the following, it might give you some ideas on how to work through problems like this:

http://androidforums.com/threads/please-read-me-before-posting.987318/
 
Back
Top Bottom