0

Im new to android studio and this is my first project. I have created a simple calculator to add 2 numbers theres no error in code but whenever im trying to run the app it always crashes

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    EditText et1,et2;
    Button bu1,bu2,bu3,bu4;
    double val,val2,result;
    TextView v1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         et1=(EditText)findViewById(R.id.editText);
        et2=(EditText)findViewById(R.id.editText2);
        v1=(TextView)findViewById(R.id.t3);


        bu1=findViewById(R.id.b1);
        bu2=findViewById(R.id.b2);
        bu3=findViewById(R.id.b3);
        bu4=findViewById(R.id.b4);
        val = Double.parseDouble(et1.getText().toString());
        val2 = Double.parseDouble(et2.getText().toString());
        bu1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                result=val+val2;

                v1.setText(Double.toString(result));
            }
        });

    }
  • What exception are you getting? – Caramiriel Feb 02 '18 at 21:56
  • @Caramiriel `FATAL EXCEPTION: main Process: com.example.naji.simplecalculator, PID: 4525 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.naji.simplecalculator/com.example.naji.simplecalculator.MainActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) ` – Minhaj Shafqat Feb 02 '18 at 21:57
  • `AppCompatTextView cannot be cast to EditText`: One of the controls you think is an `EditText` is actually a `TextView`. Double check what controls are being used for each id. – Caramiriel Feb 02 '18 at 22:02
  • @Caramiriel I removed textview but still same problem – Minhaj Shafqat Feb 02 '18 at 22:14

0 Answers0