1

Whenever I try to press a radio button on my emulator, it just force closes!

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);
Pentium10
  • 204,586
  • 122
  • 423
  • 502
neos300
  • 11
  • 1

2 Answers2

2

first of all, look at the error (DDMS button, if you use eclipse) in the DDMS console. There are a lot of reason for such error. Practically it mean, that there is unhandled java exception.

Alexander Mikhaylov
  • 1,790
  • 1
  • 13
  • 23
2

my guess that you are getting an Exception due to the value that you pass to Integer.parseInt()

you need to validate the string before parsing it.

to validate the string read the following post: Java library to check whether a String contains a number without exceptions

Community
  • 1
  • 1
Baget
  • 3,318
  • 1
  • 24
  • 44
  • How would I validate the string? Is there a function I can use? – neos300 Mar 20 '10 at 20:45
  • There are few ways, Check this post: http://stackoverflow.com/questions/1163615/java-library-to-check-whether-a-string-contains-a-number-without-exceptions – Baget Mar 21 '10 at 12:24