简单的计算器由于空指针而崩溃

时间:2013-06-18 16:34:51

标签: java android

我刚刚开始研究Android开发,我按照教程如何制作一个超级简单的基本计算器(只有sum,sub,div和mult操作)但我的APP充满了bug,其中一个当我执行没有任何插入值的操作时崩溃。

这只是代码的一部分,我认为崩溃是在这部分(对于每个按钮)。如何在不崩溃应用程序的情况下解决此问题?当一个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)

1 个答案:

答案 0 :(得分: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;
}