每当我试图按下模拟器上的单选按钮时,它就会强制关闭!
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button)this.findViewById(R.id.btn_confirm);
b.setOnClickListener(this);
RadioButton radio_ctf = (RadioButton) findViewById(R.id.radio_ctf);
RadioButton radio_ftc = (RadioButton) findViewById(R.id.radio_ftc);
radio_ctf.setOnClickListener(this);
radio_ftc.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
TextView tv = (TextView)this.findViewById(R.id.tv_result);
EditText et = (EditText)this.findViewById(R.id.et_name);
RadioButton radio_ctf = (RadioButton) findViewById(R.id.radio_ctf);
RadioButton radio_ftc = (RadioButton) findViewById(R.id.radio_ftc);
double y = 0;
int x = Integer.parseInt(et.getText().toString());
if(radio_ctf.isChecked())
{
y = ((double)x * 1.8) + 32;
}
if(radio_ftc.isChecked())
{
y = ((double)x - 32) * 0.555;
}
String text = "Result:" + y;
tv.setText(text);发布于 2010-03-21 04:41:48
首先,查看DDMS控制台中的错误(如果使用eclipse,则为DDMS按钮)。造成这种错误的原因很多。实际上,这意味着存在未处理java异常。
发布于 2010-03-21 04:43:29
我的猜测是,由于传递给Integer.parseInt()的值导致出现异常
您需要在解析字符串之前对其进行验证。
要验证该字符串,请阅读以下帖子:Java library to check whether a String contains a number exceptions
https://stackoverflow.com/questions/2484386
复制相似问题