单选复选框问题

时间:2013-07-09 09:01:36

标签: android checkbox

我试图解决我的项目活动的一个问题,所以在谷歌搜索后我找到了single selection checkedbox的例子

我应用了它,但是当它运行时它给力关闭,任何帮助解决,将受到高度赞赏,

非常感谢。

SingleSelectionCheckBox类

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;
  }
}

}
};}

主要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)

3 个答案:

答案 0 :(得分:1)

我建议你致电:

setContentView(R.layout.activity_main);
在绑定组件之前,在onCreate方法中

。 请试一试:

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

答案 1 :(得分:1)

现在使用新版Android Studio,您必须更改:

private OnCheckedChangeListener listener = new OnCheckedChangeListener()

要:

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

答案 2 :(得分:0)

找不到您的复选框。这就是你得到NullPointerException的原因。 使用setContentView()方法中的onCreate()命令设置xml布局。