简体   繁体   中英

App keeps on crashing and shows this error java.lang.IllegalStateException: Could not execute method for android:onClick

I am trying to create a simple calculator app.

The app works fine for the first input but it crashes when i want a second input and it keeps on showing this error any help would be appreciated:

java.lang.IllegalStateException: Could not execute method for android:onClick

--

Full Crash Log:

 E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.example.shivamgupta.calculator, PID: 29956
 java.lang.IllegalStateException: Could not execute method for android:onClick
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
      at android.view.View.performClick(View.java:6261)
      at android.widget.TextView.performClick(TextView.java:11180)
      at android.view.View$PerformClick.run(View.java:23748)
      at android.os.Handler.handleCallback(Handler.java:751)
      at android.os.Handler.dispatchMessage(Handler.java:95)
      at android.os.Looper.loop(Looper.java:154)
      at android.app.ActivityThread.main(ActivityThread.java:6776)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
 Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Method.invoke(Native Method)
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
      at android.view.View.performClick(View.java:6261) 
      at android.widget.TextView.performClick(TextView.java:11180) 
      at android.view.View$PerformClick.run(View.java:23748) 
      at android.os.Handler.handleCallback(Handler.java:751) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:154) 
      at android.app.ActivityThread.main(ActivityThread.java:6776) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
 Caused by: java.lang.NumberFormatException: For input string: "44335335335"
      at java.lang.Integer.parseInt(Integer.java:524)
      at java.lang.Integer.valueOf(Integer.java:611)
      at com.example.shivamgupta.calculator.MainActivity.equalto(MainActivity.java:148)
      at java.lang.reflect.Method.invoke(Native Method) 
      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 

Below is the java code( MainActivity.java ):

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

public class MainActivity extends AppCompatActivity {
    public String x ="";
    public String inp1 ="";
    public String inp2 ="";
    public String opr = "";

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

    public void Addition(View view) {
        Button addbtn = (Button) view;
        x = x + "+";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void Multiplication(View view) {
        Button mulbtn = (Button) view;
        x = x + "*";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);

    }

    public void Division(View view) {
        Button divbtn = (Button) view;
        x = x + "/";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);

    }

    public void Subtract(View view) {
        Button subbtn = (Button) view;
        x = x + "-";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);

    }

    public void seven(View view) {
        Button b = (Button) view;
        x = x + "7";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void eight(View view) {
        Button b = (Button) view;
        x = x + "8";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void nine(View view) {
        Button b = (Button) view;
        x = x + "9";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void four(View view) {
        Button b = (Button) view;
        x = x + "4";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void five(View view) {
        Button b = (Button) view;
        x = x + "5";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void six(View view) {
        Button b = (Button) view;
        x = x + "6";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void one(View view) {
        Button b = (Button) view;
        x = x + "1";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void two(View view) {
        Button b = (Button) view;
        x = x + "2";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void three(View view) {
        Button b = (Button) view;
        x = x + "3";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void zero(View view) {
        Button b = (Button) view;
        x = x + "0";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText(x);
    }

    public void equalto(View view) {
        Button btn = (Button) view;

        int i=0;

        while(x.charAt(i) != '+' && x.charAt(i) != '*' && x.charAt(i) != '/' && x.charAt(i) != '-')
        {
            inp1 =inp1 + x.charAt(i);
            i++;
        }

        opr = opr + x.charAt(i);
        i++;

        int len = x.length();

        while(x.charAt(i) != x.charAt(len -1))
        {
            inp2 =inp2 + x.charAt(i);
            i++;
        }

        inp2 =inp2 + x.charAt(i);

        Integer firstInput = Integer.valueOf(inp1);
        Integer secondInput = Integer.valueOf(inp2);

        TextView rtv = (TextView) findViewById(R.id.RES);

        if (opr.equalsIgnoreCase("+"))
        {

                    Integer result = firstInput+secondInput;

                    rtv.setText(""+result);

        }
        if (opr.equalsIgnoreCase("*"))
        {

                    Integer result = firstInput*secondInput;

                    rtv.setText(""+result);

        }
        if(opr.equalsIgnoreCase("/"))
        {

                    if(secondInput==0)
                    {
                        String mystring = "invalid";
                        Toast.makeText(getApplicationContext(), mystring , 10).show();
                    }
                    else {
                        Integer result = firstInput / secondInput;
                        rtv.setText("" + result);
                    }


        }
        if (opr.equalsIgnoreCase("-"))
        {

                    Integer result = firstInput-secondInput;

                    rtv.setText(""+result);

        }

    }

    public void clear(View view) {
        Button b = (Button) view;
        x = "";
        TextView tv = (TextView) findViewById(R.id.inp);
        tv.setText("");
        TextView rtv = (TextView) findViewById(R.id.RES);
        rtv.setText("");
    }
}

Caused by: java.lang.NumberFormatException: For input string: "44335335335" at java.lang.Integer.parseInt(Integer.java:524)

It is unable to convert your big input 44335335335 to Integer because it is out of Integers max value ( 2,147,483,647 )

Change it to Double.parseDouble , This will fix the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM