1

我刚刚开始研究 Android 开发,我遵循了一个关于如何制作一个超级简单的基本计算器(只有 sum、sub、div 和 mult 操作)的教程,但是我的 APP 充满了错误,其中一个在崩溃时崩溃我做了一个没有任何插入值的操作。

这只是代码的一部分,我认为崩溃在这部分(对于每个按钮)。如何在不使应用程序崩溃的情况下解决此问题?就像当一个 TextEdit 为空白时什么都不做?我需要任何抛出/捕获异常吗?

        sub.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                firstNum = new BigDecimal(firstNumber.getText().toString());
                secondNum = new BigDecimal(secNumber.getText().toString());

                if (firstNum == null || secondNum == null) {
                    while (verificar) {
                    }
                } else {
                    result.setText(firstNum.subtract(secondNum).toString());
                }
            }
        });

错误的logcat.txt...感谢您的耐心等待...

06-18 16:39:51.018: E/AndroidRuntime(621): FATAL EXCEPTION: main
06-18 16:39:51.018: E/AndroidRuntime(621): java.lang.NumberFormatException: Bad offset/length: offset=0 len=0 in.length=0
06-18 16:39:51.018: E/AndroidRuntime(621):  at java.math.BigDecimal.<init>(BigDecimal.java:282)
06-18 16:39:51.018: E/AndroidRuntime(621):  at java.math.BigDecimal.<init>(BigDecimal.java:438)
06-18 16:39:51.018: E/AndroidRuntime(621):  at com.example.simplecalculator.Calculadora$1.onClick(Calculadora.java:44)
06-18 16:39:51.018: E/AndroidRuntime(621):  at android.view.View.performClick(View.java:4084)
06-18 16:39:51.018: E/AndroidRuntime(621):  at android.view.View$PerformClick.run(View.java:16966)
06-18 16:39:51.018: E/AndroidRuntime(621):  at android.os.Handler.handleCallback(Handler.java:615)
06-18 16:39:51.018: E/AndroidRuntime(621):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 16:39:51.018: E/AndroidRuntime(621):  at android.os.Looper.loop(Looper.java:137)
06-18 16:39:51.018: E/AndroidRuntime(621):  at android.app.ActivityThread.main(ActivityThread.java:4745)
06-18 16:39:51.018: E/AndroidRuntime(621):  at java.lang.reflect.Method.invokeNative(Native Method)
06-18 16:39:51.018: E/AndroidRuntime(621):  at java.lang.reflect.Method.invoke(Method.java:511)
06-18 16:39:51.018: E/AndroidRuntime(621):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-18 16:39:51.018: E/AndroidRuntime(621):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-18 16:39:51.018: E/AndroidRuntime(621):  at dalvik.system.NativeStart.main(Native Method)
06-18 16:41:07.109: E/Trace(644): error opening trace file: No such file or directory (2)
06-18 16:41:09.339: E/AndroidRuntime(644): FATAL EXCEPTION: main
06-18 16:41:09.339: E/AndroidRuntime(644): java.lang.NumberFormatException: Bad offset/length: offset=0 len=0 in.length=0
06-18 16:41:09.339: E/AndroidRuntime(644):  at java.math.BigDecimal.<init>(BigDecimal.java:282)
06-18 16:41:09.339: E/AndroidRuntime(644):  at java.math.BigDecimal.<init>(BigDecimal.java:438)
06-18 16:41:09.339: E/AndroidRuntime(644):  at com.example.simplecalculator.Calculadora$1.onClick(Calculadora.java:44)
06-18 16:41:09.339: E/AndroidRuntime(644):  at android.view.View.performClick(View.java:4084)
06-18 16:41:09.339: E/AndroidRuntime(644):  at android.view.View$PerformClick.run(View.java:16966)
06-18 16:41:09.339: E/AndroidRuntime(644):  at android.os.Handler.handleCallback(Handler.java:615)
06-18 16:41:09.339: E/AndroidRuntime(644):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-18 16:41:09.339: E/AndroidRuntime(644):  at android.os.Looper.loop(Looper.java:137)
06-18 16:41:09.339: E/AndroidRuntime(644):  at android.app.ActivityThread.main(ActivityThread.java:4745)
06-18 16:41:09.339: E/AndroidRuntime(644):  at java.lang.reflect.Method.invokeNative(Native Method)
06-18 16:41:09.339: E/AndroidRuntime(644):  at java.lang.reflect.Method.invoke(Method.java:511)
06-18 16:41:09.339: E/AndroidRuntime(644):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-18 16:41:09.339: E/AndroidRuntime(644):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-18 16:41:09.339: E/AndroidRuntime(644):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

2

如果字符串为空,BigDecimal 的赋值将引发 NumberFormatException。改变这个:

firstNum = new BigDecimal(firstNumber.getText().toString());
secondNum = new BigDecimal(secNumber.getText().toString());

对此:

try {
    firstNum = new BigDecimal(firstNumber.getText().toString());
} catch (NumberFormatException e) {
    firstNum = null;
}
try {
    secondNum = new BigDecimal(secNumber.getText().toString());
} catch (NumberFormatException e) {
    secondNum = null;
}
于 2013-06-18T16:44:16.620 回答