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

时间:2015-05-18 13:22:35

标签: 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: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>

在Java中:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final RelativeLayout rl = (RelativeLayout) findViewById(R.id.layout);
    ToggleButton tb = (ToggleButton) findViewById(R.id.toggleButton);


    tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                rl.setBackgroundColor(Color.BLACK);
            } else {
                rl.setBackgroundColor(Color.WHITE);
            }
        }
    });

    CheckBox cb = (CheckBox) findViewById(R.id.checkbox);
    final TextView tv = (TextView) findViewById(R.id.textView);
    cb.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            boolean checked = ((CheckBox) v).isChecked();
            if (checked) {
                tv.setText("You want pizza with cheese");
            } else {
                tv.setText("Oh! I see. you don't like cheese.");
            }
        }
    });
}

问题是,每次我测试应用程序时都会崩溃,我找不到原因。

所以如果有人知道为什么,请帮助。

2 个答案:

答案 0 :(得分:0)

要使用此侦听器:

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

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

   }
   }
);

答案 1 :(得分:0)

您在活动中定义了CheckBox以下一行

CheckBox cb = (CheckBox) findViewById(R.id.checkbox);

但是在您的布局中,您将其ID定义为checkBox

将代码更改为以下内容应解决此问题

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

您还应该使用CompoundButton.OnCheckedChangeListener()来确定CheckBox州的更改时间:

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.");
           }
       }
   }
);