2010-03-20 81 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