[当我将EditText字段留空时,应用程序崩溃

问题描述 投票:-1回答:1

我需要帮助!将EditText保留为空会导致我的应用崩溃。我已经通过了关于stackflow和google的所有解决方案,似乎没有任何解决方案。Logcat中的两个错误是:

java.lang.reflect.InvocationTargetException

java.lang.NumberFormatException: Invalid double: ""
package com.example.curencyconverter;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {


    @SuppressLint("DefaultLocale")
    public void convert(View view) {
        EditText inputEditText = (EditText) findViewById(R.id.inputEditText);
        double inputAmount = Double.parseDouble(inputEditText.getText().toString());
        try {
            inputAmount = Double.parseDouble(inputEditText.getText().toString());
        } catch (NumberFormatException e) {
            inputAmount = Double.parseDouble(null);
        }

        Double poundAmount = inputAmount * 0.75;
        Toast.makeText(MainActivity.this, "£" + String.format("%.2f", poundAmount), Toast.LENGTH_LONG).show();
        Log.i("Amount", inputEditText.getText().toString());
    }



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

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/inputEditText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="@string/enter_amount"
        android:importantForAutofill="no"
        android:inputType="text"
        android:textAllCaps="false"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        android:textColor="#F10606"
        android:textColorHint="#006064"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.07" />

    <Button
        android:id="@+id/converter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="convert"
        android:text="@string/converter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/inputEditText"
        app:layout_constraintVertical_bias="0.0" />

</androidx.constraintlayout.widget.ConstraintLayout>

java android android-edittext
1个回答
0
投票

try-catch没有做任何事情,因为在初始化变量时您已经不安全地(没有try-catch)调用了parseDouble。只需将其初始化为0或所需的任何默认值即可。

double inputAmount = 0;

此外,parseDouble(null)生成一个NullPointerException。在inputAmount中将catch设置为默认值。

© www.soinside.com 2019 - 2024. All rights reserved.