无法解决抛出的异常?

时间:2014-03-20 13:09:47

标签: java android

这是定义所需变量的代码

public void ProcessInput(View v) {
    Button btn = (Button) v;
    String text = btn.getText().toString();
    String currentInput = inputText.getText().toString();
    int currentInputLen = currentInput.length();
    String evalResult = null;
    double userInputValue = Double.NaN;

这是我无法解决的代码

 else if (btn.getText().equals("sqrt")) {

        double LeftVal = Double.parseDouble(currentInput);
        double result = 0;
        if (currentInputLen > 0 && currentInput != "0") {
            result = Math.sqrt(LeftVal);
        }
        inputText.setText(result + "");

 else if (btn.getText().equals("sqr")) {
        if(currentInput.equals(Double.NaN+"") || currentInput.equals(Double.NEGATIVE_INFINITY+""))
            currentInput = BigDecimal.ZERO+"";
        BigDecimal LeftVal = new BigDecimal(currentInput);
        BigDecimal result = null;
        if (currentInputLen > 0 && LeftVal.compareTo(BigDecimal.ZERO) > 0) {
            result = LeftVal.pow(2);
        }

        inputText.setText(result + "");
    }

我计算了6 !,然后计算了sininverse(6!),当我按下方形按钮后,给出了NaN,应用程序崩溃了。

我想通过将double.Nan和double.negative_infinity转换为字符串并将其设置为零来检查当前输入是否无效但是它没有用完。这里出了什么问题。

03-20 18:46:34.951: E/AndroidRuntime(11491): FATAL EXCEPTION: main
03-20 18:46:34.951: E/AndroidRuntime(11491): java.lang.IllegalStateException: Could not execute method of the activity
03-20 18:46:34.951: E/AndroidRuntime(11491):    at android.view.View$1.onClick(View.java:3607)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at android.view.View.performClick(View.java:4212)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at android.view.View$PerformClick.run(View.java:17476)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at android.os.Handler.handleCallback(Handler.java:800)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at android.os.Handler.dispatchMessage(Handler.java:100)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at android.os.Looper.loop(Looper.java:194)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at android.app.ActivityThread.main(ActivityThread.java:5371)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.lang.reflect.Method.invoke(Method.java:525)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at dalvik.system.NativeStart.main(Native Method)
03-20 18:46:34.951: E/AndroidRuntime(11491): Caused by: java.lang.reflect.InvocationTargetException
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.lang.reflect.Method.invokeNative(Native Method)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.lang.reflect.Method.invoke(Method.java:525)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at android.view.View$1.onClick(View.java:3602)
03-20 18:46:34.951: E/AndroidRuntime(11491):    ... 11 more
03-20 18:46:34.951: E/AndroidRuntime(11491): Caused by: java.lang.NumberFormatException: Invalid long: "null"
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.lang.Long.invalidLong(Long.java:125)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.lang.Long.parse(Long.java:362)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.lang.Long.parseLong(Long.java:353)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.lang.Long.parseLong(Long.java:319)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.math.BigDecimal.<init>(BigDecimal.java:350)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at java.math.BigDecimal.<init>(BigDecimal.java:434)
03-20 18:46:34.951: E/AndroidRuntime(11491):    at com.example.calculator.MainActivity.ProcessInput(MainActivity.java:416)
03-20 18:46:34.951: E/AndroidRuntime(11491):    ... 14 more

3 个答案:

答案 0 :(得分:0)

尝试比较Double值而不是String值...类似于:

double doubleInput = Double.parseDouble(currentInput);
if (doubleInput == Double.NaN || doubleInput == Double.NEGATIVE_INFINITY)
    currentInput = BigDecimal.ZERO.toString();

答案 1 :(得分:0)

而是可以将您的DoubleBigDecimal转换为String,使用BigDecimal(String)构造函数将您的currentInput直接转换为BigDecimal

如果输入无法解析为数字,则会抛出NumberFormatException,您希望捕获以强制推导BigDecimal.ZERO

此外,将数字转换为字符串应该这样做:
String myDouble = String.valueOf(aDouble)(更多关于here

答案 2 :(得分:0)

您的currentInput设置为0(if中的小else if)的小解决方法似乎没有被调用,因为以下代码不会崩溃:

BigDecimal leftVal = new BigDecimal(BigDecimal.ZERO + "");

此外,异常显示您将字符串"null"传递给BigDecimal构造函数,该构造函数无法将其解析为long。不知何故,currentInput在您调用该构造函数时等于"null"。您应该尝试找到为此值分配的位置。

注意: currentInput不是null,而是等于字符串"null"