我在Android中制作了一个复选框程序,我正在使用Eclipse作为IDE。但它给出了一个错误并且“R.java”文件被自动删除,任何人都可以解决我的问题吗?谢谢你的进步。
这是我的计划:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class Checkbx extends Activity {
private CheckBox chkIos, chkAndroid, chkWindows;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnChkIos();
addListenerOnButton();
}
public void addListenerOnChkIos() {
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkIos.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
Toast.makeText(checkbx.this,
"Bro, try Android :)", Toast.LENGTH_LONG).show();
}
}
});
}
public void addListenerOnButton() {
chkIos = (CheckBox) findViewById(R.layout.chkIos);
chkAndroid = (CheckBox) findViewById(R.layout.chkAndroid);
chkWindows = (CheckBox) findViewById(R.layout.chkWindows);
btnDisplay = (Button) findViewById(R.layout.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
//Run when button is clicked
@Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("IPhone check : ").append(chkIos.isChecked());
result.append("\nAndroid check : ").append(chkAndroid.isChecked());
result.append("\nWindows Mobile check :").append(chkWindows.isChecked());
Toast.makeText(checkbx.this, result.toString(),
Toast.LENGTH_LONG).show();
}
});
}
}
main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/chkIos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_ios" />
<CheckBox
android:id="@+id/chkAndroid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_android"
android:checked="true" />
<CheckBox
android:id="@+id/chkWindows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chk_windows" />
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />
</LinearLayout>
答案 0 :(得分:0)
这是错误的
chkIos = (CheckBox) findViewById(R.layout.chkIos);
chkAndroid = (CheckBox) findViewById(R.layout.chkAndroid);
chkWindows = (CheckBox) findViewById(R.layout.chkWindows);
btnDisplay = (Button) findViewById(R.layout.btnDisplay);
像这样改变
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
chkWindows = (CheckBox) findViewById(R.id.chkWindows);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
答案 1 :(得分:0)
改变这个:
chkIos = (CheckBox) findViewById(R.layout.chkIos);
chkAndroid = (CheckBox) findViewById(R.layout.chkAndroid);
chkWindows = (CheckBox) findViewById(R.layout.chkWindows);
btnDisplay = (Button) findViewById(R.layout.btnDisplay);
到此:
chkIos = (CheckBox) findViewById(R.id.chkIos);
chkAndroid = (CheckBox) findViewById(R.id.chkAndroid);
chkWindows = (CheckBox) findViewById(R.id.chkWindows);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
你发现id不是布局。你写了R.layout.--
,你需要R.id.--