samedi 15 octobre 2016

Android If statement fails

Im using this simple if/else statement to check which radio button is selected and set the text of a textview regarding the selection. Well, for the first two cases ( if rb1.isChecked() and if rb2.isChecked() ) the code executes correctly and it just makes a calculation of two number. However, when the third case comes out (!rb1.isChecked() && !rb2.isChecked()) the application crashes. What is wrong?

        if (rb1.isChecked()) {
        int sum = x + y;
        String result = String.valueOf(sum);
        tv3.setText(result);
    } else if (rb2.isChecked()) {
        int rest = x - y;
        String result = String.valueOf(rest);
        tv3.setText(result);
    } else if (!rb1.isChecked() && !rb2.isChecked()){
        tv3.setText("Select an option!");
    }

Aucun commentaire:

Enregistrer un commentaire