问题描述
|
我试图对单选按钮使用switch语句,但运行时出现错误。有人可以帮我吗我在下面粘贴了我的android代码。如果您知道答案,请提供帮助。非常感谢。
package apt.tutorial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.R;
public class LunchtimeActivity extends Activity {
Restaurant r=new Restaurant();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button save=(Button)findViewById(R.id.button1);
save.setonClickListener(onSave);
}
private View.OnClickListener onSave=new View.OnClickListener() {
public void onClick(View v) {
EditText name=(EditText)findViewById(R.id.text1);
EditText address=(EditText)findViewById(R.id.text2);
r.setName(name.getText().toString());
r.setAddress(address.getText().toString());
RadioGroup types=(RadioGroup)findViewById(R.id.types);
switch (types.GetCheckedRadiobuttonId()) {
case R.id.Sit-Down:
r.setType(\"Sit_Down\");
break;
case R.id.Take-Out:
r.setType(\"Take_Out\");
break;
case R.id.Delivery:
r.setType(\"Delivery\");
break;
}
}
};
}
解决方法
我可以看到以下问题:
您已经导入了android.R类,该类显然没有这些字段(例如Sit-Down等
您正在调用不存在的RadioGroup方法,从
GetCheckedRadiobuttonId
更改为getCheckedRadioButtonId
Id \只能包含Java变量名称中允许的字符,因此“ 3”不是有效的标识符。
, 我认为问题出在这里:
case R.id.Sit-Down:
case R.id.Take-Out:
重命名此ID而不使用\“-\”。
, 您需要捕获视图中的单选按钮以及单选组
RadioButton sitdown = (RadioButton) findViewByid(R.id.Sit-Down);
等等