Java Android复选框getchecked(CompoundButton.OnCheckedChangeListener(无按钮单击事件))
以下是我的主要活动:Java Android复选框getchecked(CompoundButton.OnCheckedChangeListener(无按钮单击事件)),java,android,checkbox,android-checkbox,Java,Android,Checkbox,Android Checkbox,以下是我的主要活动: public class MainActivity extends ActionBarActivity implements CompoundButton.OnCheckedChangeListener { CheckBox cb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
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>
你能指引我吗?如果不想使用按钮单击侦听器,如何使用复选框
提前感谢:)使用
MainActivity。在调用setContentView
后,使用此而不是super
从MainActivity
活动的当前布局获取视图:
cb = (CheckBox) MainActivity.this.findViewById(R.id.checkBox);
用淡水洗脸后,我发现我给错了身份证
而不是
R.id.checkBox
我曾经
R.id.checkbox
您正在使用获取复选框
findViewById(R.id.checkbox);
在xml中,id是
android:id="@+id/checkBox"
id区分大小写,因此请检查大小写。基本上,您的代码是按ID获取视图,而没有找到它。因此,cb对象设置为null,并在此处抛出null指针
cb.setOnCheckedChangeListener(this);
在初始化复选框之后。添加这段代码以消除空引用
assert checkBox != null;
我试过用“this”和“main activity.this.”但还是同一个空指针异常。@OmarBISTAMI:您确定复选框在activity\u main.xml
layout中吗?非常感谢您的帮助。。。我的id是:复选框不是复选框。。。我想我需要一杯咖啡:)谢谢大家
assert checkBox != null;