单击单选按钮时强制关闭
问题描述:
每当我尝试按下模拟器上的单选按钮时,它只会强制关闭!单击单选按钮时强制关闭
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);
答
首先,在DDMS控制台中查看错误(DDMS按钮,如果使用eclipse)。 这种错误有很多原因。实际上,这意味着,有未处理的Java异常。
答
我猜你得到一个例外,由于你传递给的Integer.parseInt()的值
你需要分析它之前验证字符串。
验证字符串阅读下面的帖子: Java library to check whether a String contains a number without exceptions
大概的东西是空在那里。使用调试器和LogCat找到问题。 – Pentium10 2010-03-20 19:31:47