我正在尝试创建一个简单的计算器应用程序。
该应用程序适用于第一个输入,但它在我想要第二个输入时崩溃并且它继续显示此错误任何帮助将不胜感激:
java.lang.IllegalStateException:无法执行方法 android:onClick
-
完整崩溃日志:
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)
以下是java代码(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("");
}
}
答案 0 :(得分:2)
引起:java.lang.NumberFormatException:对于输入字符串: " 44335335335"在java.lang.Integer.parseInt(Integer.java:524)
无法将您的大输入44335335335
转换为整数,因为它超出了整数最大值(2,147,483,647
)
将其更改为Double.parseDouble
,这样可以解决问题。