过去几天/周,我一直在努力解决这个问题。
按任意操作员按钮
Here is a video producing the problem (hosted on vid.me ~34 MB)
错误
铊组成; dr
整个错误
04-04 17:55:50.906 8497-8497/com.andela.currencycalc E/AndroidRuntime: FATAL EXCEPTION: main
Process:com.andela.currencycalc, PID: 8497java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NumberFormatException: Invalid float: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseFloat(StringToReal.java:308)
at java.lang.Float.parseFloat(Float.java:306)
at com.andela.currencycalc.math.MathOperations.equals(MathOperations.java:53)
at com.andela.currencycalc.buttons.ButtonHandler.equalsButtonClicked(ButtonHandler.java:61)
at com.andela.currencycalc.MainActivity.equalsButtonClickHandler(MainActivity.java:36)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
答案 0 :(得分:0)
引起:java.lang.NumberFormatException:无效的浮点数:“”
...
在com.andela.currencycalc.math.MathOperations.equals( MathOperations.java:53 )
...
您正在使用空字符串作为参数调用Float.parseFloat(string)。
您应该在此方法之前检查输入或捕获该异常并以有意义的方式处理该情况 或者您可以尝试找到其他方法来保证输入是有效的浮点数。
答案 1 :(得分:0)
从你的代码我可以说你在这里做错了所以用MathOperations类的equals方法替换你的代码
if(!TextUtils.isEmpty(displayHandler.getDisplay().getText().toString())) {
displayHandler.addToSecondDisplay(buttonHandler.getOperandSpinner().getSelectedItem().toString(),
Float.parseFloat(displayHandler.getDisplay().getText().toString()), "");
calculate();
}
这将在解析之前检查null和空文本。