Rohan Bojja
Rohan Bojja

Reputation: 675

Calculation with two Strings, Android

I'm trying to create a calculator for Android using Eclipse. This is my code for the function 'cal', 'cal' is executed when a button is clicked, But clicking on the button closes the application, I tried an other button with different function and it works fine. Can anyone point out the mistake that I've done?

      public void cal( View view ){
            EditText op = (EditText) findViewById(R.id.editText2);
            EditText n1 = (EditText) findViewById(R.id.editText1);
            EditText n2 = (EditText) findViewById(R.id.editText3);
            EditText res = (EditText) findViewById(R.id.textView1);

             String sn1 = n1.getText().toString();
             String sn2 = n2.getText().toString();
             String sres;
             String sop;

            int in1 = Integer.parseInt(sn1);
            int in2 = Integer.parseInt(sn2);
            int ires;


                 sop = op.getText().toString();
                     if(sop == "+"){
                                  ires = in1 + in2;
                                  sres = Integer.toString(ires);
                                  res.setText(sres);

        }
}

Upvotes: 1

Views: 1211

Answers (1)

Ajay S
Ajay S

Reputation: 48622

There is no exception in this.

I think you forgot to set the activity layout which causes your application force close.

or

Problem here

EditText res = (EditText) findViewById(R.id.textView1);

Are you sure your editText name is textView1 in your layout.

If it textView then do like this.

TextView res = (TextView) findViewById(R.id.textView1);

Upvotes: 2

Related Questions