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

修复Android Studio开发的简易计算器输入为空时崩溃问题

安卓简易计算器空输入崩溃解决方案

崩溃原因

你当前的代码在点击运算按钮时,会直接读取EditText的内容并转换为Int类型,若输入框为空,etn.getText().toString()得到的是空字符串,Integer.parseInt()无法解析空字符串,会抛出未捕获的NumberFormatException异常,最终导致应用崩溃。

解决方案

利用EditText自带的setError()方法实现你需要的提示效果,同时新增通用校验方法避免每个按钮重复写校验逻辑,修改后的完整MainActivity.java代码如下:

package com.mycompany.myapp;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.View.*;
import android.view.*;

public class MainActivity extends Activity 
{
    private EditText etn1;  
    private EditText etn2;
    private TextView tvResult;
    private Button btnadd , btnsub , btnmul , btndiv;

    // 新增输入校验通用方法
    private boolean isInputValid() {
        // 去除前后空格后判断是否为空
        if (etn1.getText().toString().trim().isEmpty()) {
            etn1.setError("Please enter a value");
            return false;
        }
        if (etn2.getText().toString().trim().isEmpty()) {
            etn2.setError("Please enter a value");
            return false;
        }
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        etn1 = findViewById(R.id.etFirstValue);
        etn2 = findViewById(R.id.etSecondValue);
        tvResult = findViewById(R.id.tvAns);

        Button btnadd = findViewById(R.id.btnAdd);  
        btnadd.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // 先校验输入,不合法直接终止后续逻辑
                if (!isInputValid()) {
                    return;
                }
                int n1 = Integer.parseInt(etn1.getText().toString().trim());
                int n2 = Integer.parseInt(etn2.getText().toString().trim());
                int addResult = n1 + n2;
                tvResult.setText(String.valueOf(addResult));
            }
        });

        Button btnsub = findViewById(R.id.btnSubtract);  
        btnsub.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!isInputValid()) {
                    return;
                }
                int n1 = Integer.parseInt(etn1.getText().toString().trim());
                int n2 = Integer.parseInt(etn2.getText().toString().trim());
                int subResult = n1 - n2;
                tvResult.setText(String.valueOf(subResult));
            }
        });

        Button btnmul = findViewById(R.id.btnMultiply);  
        btnmul.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!isInputValid()) {
                    return;
                }
                int n1 = Integer.parseInt(etn1.getText().toString().trim());
                int n2 = Integer.parseInt(etn2.getText().toString().trim());
                int mulResult = n1 * n2;
                tvResult.setText(String.valueOf(mulResult));
            }
        });

        Button btndiv = findViewById(R.id.btnDivide);  
        btndiv.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (!isInputValid()) {
                    return;
                }
                int n1 = Integer.parseInt(etn1.getText().toString().trim());
                int n2 = Integer.parseInt(etn2.getText().toString().trim());
                // 额外补充除数为0的校验,避免除法场景崩溃
                if (n2 == 0) {
                    etn2.setError("除数不能为0");
                    return;
                }
                int divResult = n1 / n2;
                tvResult.setText(String.valueOf(divResult));
            }
        });
    }
}

额外优化说明

你原代码中减法、乘法、除法的运算结果变量名误写为add,上述代码已经同步修正为对应语义的变量名,更便于后续维护。同时新增了trim()处理逻辑,避免用户输入空格导致的解析异常。

内容的提问来源于stack exchange,提问作者Eliot Branco

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 06:15:04