Java 当我使用复选框小部件时,应用程序崩溃

Java 当我使用复选框小部件时,应用程序崩溃,java,android,checkbox,Java,Android,Checkbox,我试图在我的android应用程序中实现一个切换按钮和一个复选框,如下所示: 在XML中: <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应用程序中实现一个切换按钮和一个复选框,如下所示:

在XML中:

<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:id="@+id/layout"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".Main">

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:textOn="Dark Background"
    android:textOff="Light Background"

    android:id="@+id/toggleButton"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="I want pizza with"
    android:id="@+id/textView"
    android:layout_below="@+id/toggleButton"
    android:layout_alignLeft="@+id/toggleButton"
    android:layout_alignStart="@+id/toggleButton"
    android:layout_marginTop="50dp" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cheese"
    android:id="@+id/checkBox"
    android:layout_below="@+id/textView"
    android:layout_alignRight="@+id/toggleButton"
    android:layout_alignEnd="@+id/toggleButton"
    android:checked="false" />

</RelativeLayout>
问题是,每次我测试应用程序时,它都会崩溃,我找不到原因


因此,如果有人知道原因,请帮助。

尝试使用此侦听器:

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {

   }
   }
);

下面一行定义了活动中的
复选框

CheckBox cb = (CheckBox) findViewById(R.id.checkbox);
但在布局中,您将其id定义为
复选框

将代码更改为以下应该可以解决此问题

CheckBox cb = (CheckBox) findViewById(R.id.checkBox);
您还应该使用
CompoundButton.OnCheckedChangeListener()
来确定您的
复选框的状态何时更改:

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
           if (isChecked) {
               tv.setText("You want pizza with cheese");
           } else {
               tv.setText("Oh! I see. you don't like cheese.");
           }
       }
   }
);    

请提供堆栈跟踪。您应该发布堆栈跟踪…但您写了:boolean checked=((CheckBox)v).isChecked()。。。为什么要使用(复选框)v)?您之前已经初始化了复选框,所以只需要布尔checked=cb.isChecked();让cb成为最终的,你说得对。谢谢如果是因为拼写错误,应用程序通常不会编译,我不认为这是这里的问题。另一个可能的问题是,在另一个布局中,id是“checkbox”…谢谢Ed。这是我没有注意到的id。
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
           if (isChecked) {
               tv.setText("You want pizza with cheese");
           } else {
               tv.setText("Oh! I see. you don't like cheese.");
           }
       }
   }
);