【发布时间】:2014-04-14 03:32:53
【问题描述】:
我正在尝试制作一个带有 3 个复选框和 1 个按钮的简单应用程序。我想为每个复选框制作一个 toast 以查看它是否被选中(单击侦听器),然后为按钮实现一个单击侦听器以发布一般消息以查看是否选中了女巫复选框。
如果我只使用它在我的手机上运行的简单按钮(Android 版本 4.1.1)放置代码,但如果我实现这 3 个复选框,它会崩溃(我收到类似“你的应用程序停止运行,单击强制关闭)。
我的 xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Checkbox
android:id= "@+id/ok_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ok_read"
/>
<Checkbox
android:id= "@+id/removed_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/misspelled"
/>
<Checkbox
android:id= "@+id/changed_checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/changed_read"
/>
<Button
android:id= "@+id/final_click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/final_click"
/>
</LinearLayout>
活动:
package com.flowerPower.SpellingTest;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class SecondActivity extends Activity {
private Button finalB;
private CheckBox ok, changed, removed;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.second);
finalB = (Button) findViewById(R.id.final_click);
ok = (CheckBox) findViewById(R.id.ok_checkbox);
changed = (CheckBox) findViewById(R.id.changed_checkbox);
removed = (CheckBox) findViewById(R.id.removed_checkbox);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(SecondActivity.this, "Your choice is OK",
Toast.LENGTH_LONG).show();
}
}
});
changed.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(SecondActivity.this, "Your choice is changed",
Toast.LENGTH_LONG).show();
}
}
});
removed.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
Toast.makeText(SecondActivity.this, "Your choice is Windows",
Toast.LENGTH_LONG).show();
}
}
});
}
}
我该怎么办?
【问题讨论】: