views:

76

answers:

2

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);
+2  A: 

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.

Demand
+2  A: 

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

Baget
How would I validate the string? Is there a function I can use?
neos300
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