android应用程序不断崩溃“不幸的是,计算器已经停止”

mftmpeh8  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(385)

当我尝试运行以下代码时,会不断收到错误日志,并显示错误消息:

03-11 16:00:08.350: E/AndroidRuntime(842): FATAL EXCEPTION: main
03-11 16:00:08.350: E/AndroidRuntime(842): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.os.Looper.loop(Looper.java:137)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-11 16:00:08.350: E/AndroidRuntime(842):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:00:08.350: E/AndroidRuntime(842):  at java.lang.reflect.Method.invoke(Method.java:511)
03-11 16:00:08.350: E/AndroidRuntime(842):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-11 16:00:08.350: E/AndroidRuntime(842):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-11 16:00:08.350: E/AndroidRuntime(842):  at dalvik.system.NativeStart.main(Native Method)
03-11 16:00:08.350: E/AndroidRuntime(842): Caused by: java.lang.NumberFormatException: Invalid double: ""
03-11 16:00:08.350: E/AndroidRuntime(842):  at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-11 16:00:08.350: E/AndroidRuntime(842):  at java.lang.StringToReal.parseDouble(StringToReal.java:248)
03-11 16:00:08.350: E/AndroidRuntime(842):  at java.lang.Double.parseDouble(Double.java:295)
03-11 16:00:08.350: E/AndroidRuntime(842):  at com.example.calculator.MainActivity.onCreate(MainActivity.java:21)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.app.Activity.performCreate(Activity.java:5104)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-11 16:00:08.350: E/AndroidRuntime(842):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-11 16:00:08.350: E/AndroidRuntime(842):  ... 11 more
03-11 16:05:08.990: E/AndroidRuntime(959): FATAL EXCEPTION: main
03-11 16:05:08.990: E/AndroidRuntime(959): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NumberFormatException: Invalid double: ""
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.os.Looper.loop(Looper.java:137)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-11 16:05:08.990: E/AndroidRuntime(959):  at java.lang.reflect.Method.invokeNative(Native Method)
03-11 16:05:08.990: E/AndroidRuntime(959):  at java.lang.reflect.Method.invoke(Method.java:511)
03-11 16:05:08.990: E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-11 16:05:08.990: E/AndroidRuntime(959):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-11 16:05:08.990: E/AndroidRuntime(959):  at dalvik.system.NativeStart.main(Native Method)
03-11 16:05:08.990: E/AndroidRuntime(959): Caused by: java.lang.NumberFormatException: Invalid double: ""
03-11 16:05:08.990: E/AndroidRuntime(959):  at java.lang.StringToReal.invalidReal(StringToReal.java:63)
03-11 16:05:08.990: E/AndroidRuntime(959):  at java.lang.StringToReal.parseDouble(StringToReal.java:248)
03-11 16:05:08.990: E/AndroidRuntime(959):  at java.lang.Double.parseDouble(Double.java:295)
03-11 16:05:08.990: E/AndroidRuntime(959):  at com.example.calculator.MainActivity.onCreate(MainActivity.java:21)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.app.Activity.performCreate(Activity.java:5104)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-11 16:05:08.990: E/AndroidRuntime(959):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-11 16:05:08.990: E/AndroidRuntime(959):  ... 11 more

主活动.java

package com.example.calculator;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

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

        TextView tPrincipalAmount   = (TextView)findViewById(R.id.tPrincipalAmount);
        EditText editPrincipalAmount= (EditText)findViewById(R.id.editPrincipalAmount);
        String String_Principal_amount = editPrincipalAmount.getText().toString();
        double DbPrincipalAmount = Double.parseDouble(String_Principal_amount);

        TextView tInterestRate =(TextView) findViewById(R.id.tInterestRate);
        EditText editInterestRate =(EditText) findViewById(R.id.editInterestRate);
        String string_editInterestRate =editInterestRate.getText().toString();
        double DbInterestRate = Double.parseDouble(string_editInterestRate);

    }

}

活动\u main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tPrincipalAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="14dp"
        android:text="Principal Amount ($)" />

    <EditText
        android:id="@+id/editPrincipalAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tPrincipalAmount"
        android:layout_below="@+id/tPrincipalAmount"
        android:ems="10" />

    <TextView
        android:id="@+id/tInterestRate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editPrincipalAmount"
        android:layout_below="@+id/editPrincipalAmount"
        android:layout_marginTop="26dp"
        android:text="Interest Rate" />

    <EditText
        android:id="@+id/editInterestRate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tInterestRate"
        android:layout_marginTop="27dp"
        android:ems="10" />

</RelativeLayout>

谢谢您

4urapxun

4urapxun1#

这个 string 要解析为 double 不包含一个值,这就是为什么您要 NumberFormatException . 你应该检查一下 string 包含有效值。现在,检查如下。。。

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

    TextView tPrincipalAmount   = (TextView)findViewById(R.id.tPrincipalAmount);
    EditText editPrincipalAmount= (EditText)findViewById(R.id.editPrincipalAmount);
    String String_Principal_amount = editPrincipalAmount.getText().toString();
    double DbPrincipalAmount = 0;

    if (String_Principal_amount != null && !String_Principal_amount.equals("")) {

        DbPrincipalAmount = Double.parseDouble(String_Principal_amount);

    }

    TextView tInterestRate =(TextView) findViewById(R.id.tInterestRate);
    EditText editInterestRate =(EditText) findViewById(R.id.editInterestRate);
    String string_editInterestRate =editInterestRate.getText().toString();

    double DbInterestRate = 0;

    if (string_editInterestRate != null && !string_editInterestRate.equals("")) {

        DbInterestRate = Double.parseDouble(string_editInterestRate);

     }

}

相关问题