【问题标题】:Force close on clicking on radio button单击单选按钮强制关闭
【发布时间】:2010-03-20 19:23:32
【问题描述】:

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

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

【问题讨论】:

  • 那里可能有些东西是空的。使用调试器和 LogCat 来定位问题。

标签: android android-emulator


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我猜你得到一个异常是由于你传递给 Integer.parseInt() 的值

    您需要在解析之前验证字符串。

    要验证字符串,请阅读以下帖子: Java library to check whether a String contains a number without exceptions

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多