Android 复选框被选中(CompoundButton.OnCheckedChangeListener(没有按钮点击事件))

Android Checkbox getchecked (CompoundButton.OnCheckedChangeListener (without button click event))

这是我的 MainActivity:

public class MainActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {

    CheckBox cb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getCheckBox();
        /*
        cb = (CheckBox) super.findViewById(R.id.checkbox);
        cb.setOnCheckedChangeListener(this);
        */
    }

    public void getCheckBox(){

        cb = (CheckBox) super.findViewById(R.id.checkbox);
        cb.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(buttonView.getId()==R.id.checkBox){
            TextView tv = (TextView) findViewById(R.id.textView);
            tv.setText(buttonView.getText().toString());
        }

    }
    }

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My One"
        android:id="@+id/checkBox"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="160dp" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My Two"
        android:id="@+id/checkBox2"
        android:layout_alignTop="@+id/checkBox"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="40dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/textView"
        android:layout_below="@+id/radioGroup"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="50dp" />

</RelativeLayout>

当我 运行

时出现以下异常
02-20 15:55:28.130    2110-2110/sqllistviewdemo.wptrafficanalyzer.in.radiobutton E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: sqllistviewdemo.wptrafficanalyzer.in.radiobutton, PID: 2110
    java.lang.RuntimeException: Unable to start activity ComponentInfo{sqllistviewdemo.wptrafficanalyzer.in.radiobutton/sqllistviewdemo.wptrafficanalyzer.in.radiobutton.MainActivity}: 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:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     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 sqllistviewdemo.wptrafficanalyzer.in.radiobutton.MainActivity.getCheckBox(MainActivity.java:34)
            at sqllistviewdemo.wptrafficanalyzer.in.radiobutton.MainActivity.onCreate(MainActivity.java:27)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access0(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

你能指导我吗?如果您不想使用按钮点击侦听器,您如何使用复选框?

提前致谢:)

在调用 setContentView:

后,使用 MainActivity.this 而不是 superMainActivity Activity 的当前布局获取视图
    cb = (CheckBox) MainActivity.this.findViewById(R.id.checkBox);

用淡水洗脸后发现我填错了ID 而不是

R.id.checkBox

我用过

R.id.checkbox

您正在使用

获取 CheckBox
findViewById(R.id.checkbox); 

在 xml 时,id 是

android:id="@+id/checkBox"

ID 区分大小写,因此请检查您的大小写。基本上你的代码是通过 ID 获取视图但没有找到它。因此你的 cb 对象被设置为 null 并且你在这里抛出一个空指针

cb.setOnCheckedChangeListener(this);

初始化复选框后。添加这段代码以消除空引用。

assert checkBox != null;