You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio模拟器输入小数点后崩溃问题求助

输入小数点后Android模拟器崩溃的问题解决

我正在开发一款卡路里计算App,输入纯数字时程序运行正常,但输入小数点“.”后Android Studio模拟器就会崩溃。已在EditText组件中设置inputType为numberDecimal限制输入内容为数字或小数点,但问题仍未解决。

布局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">

    <LinearLayout
        android:layout_width="376dp"
        android:layout_height="428dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.461"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.258">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Proteínas(g)"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/txtNro1"
            android:layout_width="match_parent"
            android:layout_height="57dp"
            android:ems="10"
            android:inputType="numberDecimal"
            android:textColor="#C3BEBE" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Carboidratos(g)"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/txtNro2"
            android:layout_width="match_parent"
            android:layout_height="57dp"
            android:ems="10"
            android:inputType="numberDecimal"
            android:textColor="#C3BEBE" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Gorduras(g)"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/txtNro3"
            android:layout_width="match_parent"
            android:layout_height="61dp"
            android:ems="10"
            android:inputType="numberDecimal"
            android:textColor="#C3BEBE" />

        <Button
            android:id="@+id/btCalcular"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="Calcular"
            android:text="Calcular" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="O total de calorias é"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvResultado"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="0"
            android:textColor="#FF0000"
            android:textSize="18sp" />

    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity代码

package com.example.calorias;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private EditText txtNro1;
    private EditText txtNro2;
    private EditText txtNro3;
    private TextView tvResultado;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtNro1 = findViewById(R.id.txtNro1);
        txtNro2 = findViewById(R.id.txtNro2);
        txtNro3 = findViewById(R.id.txtNro3);
        tvResultado = findViewById(R.id.tvResultado);
    }

    public void Calcular(View view){
        double valor1 = Integer.parseInt(txtNro1.getText().toString());
        double valor2 = Integer.parseInt(txtNro2.getText().toString());
        double valor3 = Integer.parseInt(txtNro3.getText().toString());

        double v1 = (valor1 * 4) ;
        double v2 = (valor2 * 4);
        double v3 = (valor3 * 9);

        tvResultado.setText(String.valueOf(v1 + v2 + v3));
    }

}

问题原因及解决方法

崩溃的核心原因是你用了Integer.parseInt()去解析带小数点的字符串——Integer类型只能处理整数,遇到小数点会直接抛出NumberFormatException,导致程序崩溃。你需要改用Double.parseDouble()来解析包含小数的输入。

另外,还要处理输入为空或者仅输入小数点的情况,避免再次抛出异常。修改后的Calcular方法如下:

public void Calcular(View view){
    // 获取输入内容,去除首尾空格
    String input1 = txtNro1.getText().toString().trim();
    String input2 = txtNro2.getText().toString().trim();
    String input3 = txtNro3.getText().toString().trim();

    // 解析为double,处理空或仅小数点的无效输入
    double valor1 = input1.isEmpty() || input1.equals(".") ? 0 : Double.parseDouble(input1);
    double valor2 = input2.isEmpty() || input2.equals(".") ? 0 : Double.parseDouble(input2);
    double valor3 = input3.isEmpty() || input3.equals(".") ? 0 : Double.parseDouble(input3);

    double v1 = valor1 * 4;
    double v2 = valor2 * 4;
    double v3 = valor3 * 9;

    tvResultado.setText(String.valueOf(v1 + v2 + v3));
}

这样修改后,无论是整数、小数,还是空输入、仅小数点的情况,程序都能正常运行,不会崩溃。

内容的提问来源于stack exchange,提问作者Luis Tartareli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 10:55:18