【问题标题】:Android Checkbox getchecked (CompoundButton.OnCheckedChangeListener (without button click event))Android Checkbox getchecked (CompoundButton.OnCheckedChangeListener (无按钮点击事件))
【发布时间】:2015-02-20 15:58:35
【问题描述】:

这是我的 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)

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

提前非常感谢:)

【问题讨论】:

    标签: java android checkbox android-checkbox


    【解决方案1】:

    您正在使用

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

    在xml中,id是

    android:id="@+id/checkBox"
    

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

    cb.setOnCheckedChangeListener(this);
    

    【讨论】:

      【解决方案2】:

      在调用setContentView 后,使用MainActivity.this 而不是super 从MainActivity Activity 的当前布局获取视图:

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

      【讨论】:

      • 我尝试了`this`和`MainActivity.this。 ` 但仍然是相同的空指针异常。
      • @OmarBISTAMI:你确定 CheckBox 在 activity_main.xml 布局中吗?
      • 非常感谢您的帮助...我有 ID:复选框而不是复选框...我想我需要一杯咖啡 :) 非常感谢大家
      【解决方案3】:

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

      assert checkBox != null;
      

      【讨论】:

        【解决方案4】:

        用淡水洗脸后,我发现我输入了错误的 ID 而不是

        R.id.checkBox
        

        我用过

        R.id.checkbox
        

        【讨论】:

          猜你喜欢
          • 2011-12-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-08-28
          • 2023-04-09
          • 1970-01-01
          相关资源
          最近更新 更多