Android Checkbox getchecked(CompoundButton.OnCheckedChangeListener(无按钮点击事件)) [英] Android Checkbox getchecked (CompoundButton.OnCheckedChangeListener (without button click event))

查看:324
本文介绍了Android Checkbox getchecked(CompoundButton.OnCheckedChangeListener(无按钮点击事件))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的MainActivity:

  public class MainActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener {

复选框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/toolsandroid:layout_width =match_parent
android:layout_height =match_parentandroid:paddingLeft =@ dimen / activity_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
android:paddingBottom =@ dimen / activity_vertical_margintools: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 
过程:sqllistviewdemo.wptrafficanalyzer.in。 radiobutton,PID:2110
java.lang.RuntimeException:无法启动活动ComponentInfo {sqllistviewdemo.wptrafficanalyzer.in.radiobutton / sqllistviewdemo.wptrafficanalyzer.in.radiobutton.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton $ OnCheckedChangeListener)'对一个空对象引用
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
在android .app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
在android.app.ActivityThread $ H.handleMessage(ActivityThread。 java:1278)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:135)
在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:尝试在空对象引用上调用虚方法void android.widget.CheckBox.setOnCheckedChangeListener(android.widget.CompoundButton $ OnCheckedChangeListener)
at sqllistviewdemo.wptrafficanalyzer.in .radiobutton.MainActivity.getCheckBox(MainActivity.java:34)
at sqllistviewdemo.wptrafficanalyzer.in.radiobutton.MainActivity.onCreate(MainActivity.java:27)
在android.app.Activity.performCreate(Activity .java:5933)
在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
在android。 app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
在android.app.ActivityThread $ H.handleMessage(ActivityThread.java :1278)
在android.os.Handler.dispatchMessage(Handler.java:102)
在android.os.Looper.loop(Looper.java:135)
在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)

你能指导我吗?



提前感谢:)

如果您不想使用按钮点击监听器,

  findViewById(R。 id.checkbox);在xml中,ID为

时,



  android:id =@ + id / checkBox

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

  cb.setOnCheckedChangeListener 


Here is my 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());
        }

    }
    }

And here my layout :

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

I get the following exceptions when i run

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)

Can you please guide me ? how do you work with checkbox if you don't want to use a button click listener ?

Thanks a lot in advance :)

解决方案

You are fetching your CheckBox with

findViewById(R.id.checkbox); 

when in the xml, the id is

android:id="@+id/checkBox"

The id is case sensitive so check your capitalization. Basically your code is fetching a view by ID and not finding it. Therefore your cb object is set to null and you throw a nullpointer here

cb.setOnCheckedChangeListener(this);

这篇关于Android Checkbox getchecked(CompoundButton.OnCheckedChangeListener(无按钮点击事件))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆