我正在构建我的计算器应用程序。除了pow方法之外,一切正常。
当我点击8 + 5,然后点击pow方法时,我得到了这些:
calculatorat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.atlant.calculator.calculatorat, PID: 28984
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:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NumberFormatException: Invalid double: "7+7"
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.initialParse(StringToReal.java:164)
at java.lang.StringToReal.parseDouble(StringToReal.java:282)
at java.lang.Double.parseDouble(Double.java:301)
at com.atlant.calculator.calculatorat.Calculator.getResultPow(Calculator.java:178)
at com.atlant.calculator.calculatorat.Calculator.onClickPow(Calculator.java:205)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
比较字符串没有问题,我知道我可以将它们与.equals进行比较!.Problem是我的第三个getResultPow案例。
我只从getResultPow中的第3个案例中得到错误(当我尝试将两个数字相加然后点击POW时).POW在1.st和2.nd情况下正常工作(输入数字然后点击POW)。
这是我的github回购:[https://github.com/adnxy/CalculatorAt][1]
有人建议吗?
Bellow是我的onClickPow:
public void onClickPow(View v) {
if (display == "") return;
if (!getResultPow()) return;
{
_screen.setText(String.valueOf(result));
}
}
我的getResultPow方法有三种情况:1.st和2.nd情况正常,但是当我点击第三种情况时,我会收到错误。
//1.st case ,all empty, working fine
if (display == "" && currentOperator == "" && result == "") return false;
//2.nd case, entered number+pow, working fine
if (currentOperator == "" && result == "" )
if (display != "") {
result = String.valueOf(Double.parseDouble(display) * Double.parseDouble(display));
}
//3rd. case, two numbers entered and pow clicked, exception
if (result == "")
if (display != "" && currentOperator != "") {
result = String.valueOf(String.valueOf(result)+Double.parseDouble(display) * Double.parseDouble(display));
return true;
}
[1]: https://github.com/adnxy/CalculatorAt
答案 0 :(得分:0)
查看第三个错误。您正在尝试将字符串转换为7+7
的两倍。试着写:
Double pow = Double.parseDouble(display) * Double.parseDouble(display);
result = String.valueOf(Double.parseDouble(Double.parseDouble(result)+pow));