androidqq6
androidqq6

Reputation: 1526

single selection checkbox issue

Im trying to solve one problem of my project activity , so after googling i found example of single selection checkedbox

i applied it but when run it it gave force close , any help to solve that will be highly appreciated ,

thanks alot .

SingleSelectionCheckBox class

public class SingleSelectionCheckBox extends Activity {

 CheckBox cbActivate ;
 CheckBox cbAll ;
 CheckBox cbFilter ;

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 cbActivate = (CheckBox)findViewById(R.id.cbActivate);
 cbActivate.setOnCheckedChangeListener(listener);

cbAll = (CheckBox)findViewById(R.id.cbAll);
 cbAll.setOnCheckedChangeListener(listener);

cbFilter = (CheckBox)findViewById(R.id.cbFilter);
 cbFilter.setOnCheckedChangeListener(listener);

}
private OnCheckedChangeListener listener = new OnCheckedChangeListener() {

public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if(isChecked){
switch(arg0.getId())
  {
    case R.id.cbActivate:
         cbActivate.setChecked(true);
         cbAll.setChecked(false);
         cbFilter.setChecked(false);
         break;
    case R.id.cbAll:
         cbAll.setChecked(true);
         cbFilter.setChecked(false);
         cbActivate.setChecked(false);
         break;
   case R.id.cbFilter:
        cbFilter.setChecked(true);
        cbAll.setChecked(false);
        cbActivate.setChecked(false);
        break;
  }
}

}
};}

main xml

<CheckBox
 android:id="@+id/cbActivate"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="50dp"
 android:text="ActivateMe" />

<CheckBox
 android:id="@+id/cbAll"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="50dp"
 android:text="ApplyAll" />

<CheckBox
 android:id="@+id/cbFilter"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginLeft="50dp"
 android:text="ApplyFilter" />

Logcat

java.lang.RuntimeException: Unable to start activity 
 ComponentInfo{com.test.demo/com.test.demo.SingleSelectionCheckBox}:  
 java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.NullPointerException
at com.test.demo.SingleSelectionCheckBox.onCreate(SingleSelectionCheckBox.java:19)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)

Upvotes: 1

Views: 388

Answers (3)

Emre Tekin
Emre Tekin

Reputation: 472

Now with the new version Android Studio you have to change from:

private OnCheckedChangeListener listener = new OnCheckedChangeListener()

To:

private CompoundButton.OnCheckedChangeListener listener = new CompoundButton.OnCheckedChangeListener()

Upvotes: 1

KrazyMicha
KrazyMicha

Reputation: 31

Your checkboxes can't be found. That's the reason why you get a NullPointerException. Use the command setContentView() in your onCreate() method to set your xml layout.

Upvotes: 0

nano_nano
nano_nano

Reputation: 12523

I suggest you to call:

setContentView(R.layout.activity_main);

in your onCreate method before binding the components. Please try it:

public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 cbActivate = (CheckBox)findViewById(R.id.cbActivate);
 ...
}

Upvotes: 1

Related Questions