get Checkbox getchecked(CompoundButton.OnCheckedChangeListener(没有按钮点击事件))

时间:2015-02-20 15:58:36

标签: java android checkbox android-checkbox

这是我的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.access$800(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.access$800(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)
你能指导我吗?如果你不想使用按钮点击监听器,你如何使用复选框?

提前多多感谢:)

4 个答案:

答案 0 :(得分:3)

您正在使用

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

在xml中,id为

android:id="@+id/checkBox"

ID区分大小写,请检查您的大小写。基本上你的代码是通过ID获取视图而不是找到它。因此,您的cb对象设置为null,并在此处抛出nullpointer

cb.setOnCheckedChangeListener(this);

答案 1 :(得分:2)

在调用MainActivity.this后,使用super代替MainActivity获取setContentView活动当前布局的视图:

    cb = (CheckBox) MainActivity.this.findViewById(R.id.checkBox);

答案 2 :(得分:1)

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

assert checkBox != null;

答案 3 :(得分:0)

用一些淡水洗脸后我发现我给了错误的身份证 而不是

R.id.checkBox

我用过

R.id.checkbox