应用程序持续崩溃并显示此错误java.lang.IllegalStateException:无法为android执行方法:onClick

应用程序持续崩溃并显示此错误java.lang.IllegalStateException:无法为android执行方法:onClick,android,Android,我正在尝试创建一个简单的计算器应用程序 该应用程序在第一次输入时运行良好,但在我需要第二次输入时崩溃,并不断显示此错误。如有任何帮助,将不胜感激: java.lang.IllegalStateException:无法执行的方法 安卓:onClick -- 完整崩溃日志: 下面是java代码(MainActivity.java): 原因:java.lang.NumberFormatException:对于输入字符串: java.lang.Integer.parseInt(Integer.java:

我正在尝试创建一个简单的计算器应用程序

该应用程序在第一次输入时运行良好,但在我需要第二次输入时崩溃,并不断显示此错误。如有任何帮助,将不胜感激:

java.lang.IllegalStateException:无法执行的方法 安卓:onClick

--

完整崩溃日志:

下面是java代码(
MainActivity.java
):

原因:java.lang.NumberFormatException:对于输入字符串: java.lang.Integer.parseInt(Integer.java:524)中的“44335335335”

无法将大输入
44335335335
转换为整数,因为它超出了整数的最大值(
2147483647


将其更改为
Double.parseDouble
,这将解决问题。

为什么
按钮addbtn=(按钮)视图按钮添加BTN=(按钮)查看以初始化+变量ITS NumberFormatException的按钮检查您解析输入文本的逻辑。应用程序现在没有崩溃,但我仍然无法获得输出您预期的输出和您当前获得的输出是什么?例如,当我运行应用程序进行第一次输入时,让我们假设12+2我得到答案14,然后我按C键清除并输入其他内容,例如13-2我没有得到任何结果在
clear
方法中您只是清除
x
的值,同时尝试清除
inp1
inp2
opr
非常感谢应用程序现在可以运行了。你能告诉我为什么我在清除x时必须清除inp1 inp2和opr,并且所有这些变量都是从x派生的吗?
 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) 
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("");
    }
}