-2

First, i need to alert messege if user don't write any thing in edit text or user uncheck radio button alert error. but my program stopped.

Second, user can check more one radio button ?! how can make just select one radio button ?

this is my code

 public class MainActivity extends Activity {

    TextView t1;
    EditText e1,e2;
    Button b1;
    RadioButton r1,r2,r3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        e1 = (EditText) findViewById(R.id.editText1);
        e2 = (EditText) findViewById(R.id.editText2);
        b1 = (Button) findViewById(R.id.button1);
        r1 = (RadioButton) findViewById(R.id.radioButton1);
        r2 = (RadioButton) findViewById(R.id.radioButton2);
        r3 = (RadioButton) findViewById(R.id.radioButton3);
        t1 = (TextView) findViewById(R.id.textView4);

        r1.setChecked(false);
        r2.setChecked(false);
        r3.setChecked(false);


        b1.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

                int psi = Integer.parseInt(e1.getText().toString());
                int cable = Integer.parseInt(e2.getText().toString());
                final double bars = 0.7;

                int psiTen = (psi / 10);
                int bar = (int)(psiTen * bars);


                if(r1.isChecked() == true){
                    r2.setChecked(false);
                    r3.setChecked(false);
                }
                else if(r2.isChecked() == true){
                    r1.setChecked(false);
                    r3.setChecked(false);
                }
                else if(r3.isChecked() == true){
                    r2.setChecked(false);
                    r1.setChecked(false);
                }

                if(e1.getText().toString() == "" || e2.getText().toString() == "") t1.setText("Fill field,please");

                if(bar <= 400 && r1.isChecked()  && cable == 1){
                    t1.setText("Engine 1");
                }
                else if(bar >= 400 && r2.isChecked()  && cable == 1){
                    t1.setText("Engine 2");
                }
                else if(bar >= 400 && r3.isChecked()  && cable == 2){
                    t1.setText("Engine 3");
                }
                else if(bar >= 400 && r1.isChecked()  && cable == 2){
                    t1.setText("Engine 4");
                }
                else if(bar <= 400 && r2.isChecked()  && cable == 2){
                    t1.setText("Engine 5");
                }
                else if(bar >= 400 && r3.isChecked()  && cable == 2){
                    t1.setText("Engine 6");
                }
                else if(bar <= 400 && r3.isChecked()  && cable == 3){
                    t1.setText("Engine 7");
                }
                else if(bar <= 400 && r1.isChecked()  && cable == 3){
                    t1.setText("Engine 8");
                }
                else if(bar <= 400 && r2.isChecked()  && cable == 3){
                    t1.setText("Engine 9");
                }
                else if(bar >= 400 && r3.isChecked()  && cable == 3){
                    t1.setText("Engine 10");
                }
                else if(bar >= 400 && r1.isChecked()  && cable == 3){
                    t1.setText("Engine 11");
                }
                else if(bar >= 400 && r2.isChecked()  && cable == 3){
                    t1.setText("Engine 12");
                }

            }

        });
Pararth
  • 8,114
  • 4
  • 34
  • 51
  • 3
    Please consult your *Logcat* logs -- it'll help you a lot. – Lee White Jun 05 '14 at 09:33
  • 3
    http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Jun 05 '14 at 09:34
  • `how can make just select one radio button?` Enclose your RadioButtons in a RadioGroup. Each RadioGroup will allow the selection of 1 and only 1 RadioButton. – Phantômaxx Jun 05 '14 at 09:43

2 Answers2

3

The problem is here:

int psi = Integer.parseInt(e1.getText().toString());
int cable = Integer.parseInt(e2.getText().toString());  

This will throw a NumberFormatException if the value is "" or a non-number. Try checking for a blank string before you try to parse it. You are checking it but you're doing it after you actually attempt to parse. Do it before parsing.

And by the way this:

if(e1.getText().toString() == "" || e2.getText().toString() == "") t1.setText("Fill field,please");  

is not how you check for equality of strings. Use the equals() method.

To select just one RadioButton group them in a RadioGroup as described here.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
0

You can also check if the length of the Editable returned from getText is zero:

if(e1.getText().length == 0)

or convert the Editable to a string:

if(e1.getText().toString().isEmpty())

To make sure that only one radio button can be selected you will need to enclose your radio buttons in a radio group:

<RadioGroup
        android:id="@+id/active_group"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            ...
        />

        <RadioButton
            ...
        />
</RadioGroup>
todd
  • 1,266
  • 1
  • 13
  • 20