2010-03-20 85 views
1

每当我尝试按下模拟器上的单选按钮时,它只会强制关闭!单击单选按钮时强制关闭

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); 
+1

大概的东西是空在那里。使用调试器和LogCat找到问题。 – Pentium10 2010-03-20 19:31:47

回答

2

首先,在DDMS控制台中查看错误(DDMS按钮,如果使用eclipse)。 这种错误有很多原因。实际上,这意味着,有未处理的Java异常。

2

我猜你得到一个例外,由于你传递给的Integer.parseInt()的值

你需要分析它之前验证字符串。

验证字符串阅读下面的帖子: Java library to check whether a String contains a number without exceptions

+0

我将如何验证字符串?有我可以使用的功能吗? – neos300 2010-03-20 20:45:22

+0

有几种方法,检查这篇文章: http://stackoverflow.com/questions/1163615/java-library-to-check-whether-a-string-contains-a-number-without-exceptions – Baget 2010-03-21 12:24:26