Main_Activity 启动崩溃

Main_Activity Launch crashes

我正在尝试制作一个 android 应用程序,但是在开发者设备上启动主 activity 时,logcat 是这样的:

设备为三星GT-S5282: 扎根:复活混音(棉花糖)

    07-23 21:35:35.840 28372-28372/? I/art: Late-enabling -Xcheck:jni
07-23 21:35:36.460 28372-28372/com.win.rd.calculator W/System: ClassLoader referenced unknown path: /data/app/com.win.rd.calculator-1/lib/arm
07-23 21:35:36.550 28372-28382/com.win.rd.calculator I/art: Background sticky concurrent mark sweep GC freed 2992(169KB) AllocSpace objects, 0(0B) LOS objects, 52% free, 615KB/1294KB, paused 5.118ms total 79.262ms
07-23 21:35:36.610 28372-28372/com.win.rd.calculator W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
    unsupported multiclient unsolicited response code 1009
07-23 21:35:37.020 28372-28372/com.win.rd.calculator D/AndroidRuntime: Shutting down VM
07-23 21:35:37.030 28372-28372/com.win.rd.calculator E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.win.rd.calculator, PID: 28372
    Theme: themes:{}
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.win.rd.calculator/com.win.rd.calculator.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5471)
        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 double: ""
        at java.lang.StringToReal.invalidReal(StringToReal.java:63)
        at java.lang.StringToReal.parseDouble(StringToReal.java:267)
        at java.lang.Double.parseDouble(Double.java:301)
        at java.lang.Double.valueOf(Double.java:338)
        at com.win.rd.calculator.MainActivity.onCreate(MainActivity.java:35)
        at android.app.Activity.performCreate(Activity.java:7125)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2415)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2522) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:148) 
        at android.app.ActivityThread.main(ActivityThread.java:5471) 
        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) 

怎么了?

这是我的主要内容 activity:

package com.win.rd.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    EditText edt1, edt2;
    TextView txt;
    Button pls, min, mlp, div;

    double numA, numB, result;
    int flag, ecd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edt1 = (EditText) findViewById(R.id.num1);
        edt2 = (EditText) findViewById(R.id.num2);
        txt = (TextView) findViewById(R.id.res);
        pls = (Button) findViewById(R.id.plus);
        min = (Button) findViewById(R.id.minus);
        mlp = (Button) findViewById(R.id.multiply);
        div = (Button) findViewById(R.id.divide);

        numA = numB = result = flag = 0;

        numA = Double.valueOf(edt1.getText().toString());
        numB = Double.valueOf(edt2.getText().toString());

    }

    public void Operation ( double numA, double numB, int flag){
        if (flag == 1) {
            result = (numA + numB);
        } else if (flag == 2) {
            result = (numA - numB);
        } else if (flag == 3) {
            result = (numA * numB);
        } else if (flag == 4) {
            result = numA / numB;
            if (Double.isInfinite(result)) {
                ecd = 1;
                edt1.setText("");
                edt2.setText("");
                Toast.makeText(this, "Cannot Divide By Zero", Toast.LENGTH_SHORT).show();
            }
            else if (Double.isNaN(result)){
                ecd = 2;
                edt1.setText("");
                edt2.setText("");
                Toast.makeText(this, "0/0 Not possible", Toast.LENGTH_SHORT).show();
            }
        }
    }

    public void Plus (View view){
        txt.setText("+");
        flag = 1;
        if (!edt1.toString().equals("") || !edt2.toString().equals(""))
            Operation(numA, numB, flag);
        else
            Toast.makeText(this, "Empty Field", Toast.LENGTH_SHORT).show();
    }
    public void Minus (View view){
        txt.setText("-");
        flag = 2;
        if (!edt1.toString().equals("") || !edt2.toString().equals(""))
            Operation(numA, numB, flag);
        else
            Toast.makeText(this, "Empty Field", Toast.LENGTH_SHORT).show();
    }

    public void Multiply (View view){
        txt.setText("x");
        flag = 3;
        if (!edt1.toString().equals("") || !edt2.toString().equals(""))
            Operation(numA, numB, flag);
        else
            Toast.makeText(this, "Empty Field", Toast.LENGTH_SHORT).show();
    }

    public void Divide (View view){
        txt.setText("/");
        flag = 4;
        if (!edt1.toString().equals("") || !edt2.toString().equals(""))
            Operation(numA, numB, flag);
        else
            Toast.makeText(this, "Empty Field", Toast.LENGTH_SHORT).show();
    }

    public void Result (View view){
        if (flag == 4) {
            if (ecd == 1)
                txt.setText("Infinite");
            if (ecd == 2)
                txt.setText("NaN");
        }
        else
            txt.setText(String.valueOf(result));
    }
}

这是一个简单的计算器应用程序。

Double.valueOf() & Double.double.parseDouble() 产生相同的结果对吗? java 中还有哪些类型转换方法? 感谢您的快速评论。

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NumberFormatException: Invalid double: ""

错误提示您正在尝试将空字符串转换为双精度字符串,这是导致此问题的原因,因此您应该像下面这样处理空字符串情况

if(edt1.getText().toString() != null && !edt1.getText().toString().isEmpty()){
    numA = Double.valueOf(edt1.getText().toString());
}