有空字段时创建对话框

Create dialog when there is an empty field

我一直在努力解决这个问题,每当其中有一个空白字段时,我的应用程序就会崩溃。

当我 运行 代码时,它告诉我第 32 行和第 37 行有错误,就在我将字符串从 textView 转换为整数以对这些数字进行操作的地方

如果你们能看看这个就好了。

package com.studium.second.secondtest;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    Activity miActividad;

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

        miActividad = this;


        Button calcular = findViewById(R.id.button1);

        calcular.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                TextView textoBase = findViewById(R.id.areaBase);
                String base = textoBase.getText().toString();
                int numeroBase = Integer.parseInt(base);


                TextView textoAltura = findViewById(R.id.areaAltura);
                String altura = textoAltura.getText().toString();
                int numeroAltura = Integer.parseInt(altura);

                //Operación
                final TextView cajaResultado = findViewById(R.id.areaResultado);

                final int operacion = numeroAltura * numeroBase;
                cajaResultado.setText(getResources().getString(R.string.textoResultado ) + " " + operacion);

                if (base == null && altura != null){
                    AlertDialog.Builder builder = new AlertDialog.Builder(miActividad.getApplicationContext());
                    builder.setMessage("El parámetro base está vacío")
                            .setCancelable(false)
                            .setNegativeButton("Volver", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alert = builder.create();
                    alert.show();
                }

                if (base != null && altura == null){
                    AlertDialog.Builder builder = new AlertDialog.Builder(miActividad.getApplicationContext());
                    builder.setMessage("El parámetro altura está vacío")
                            .setCancelable(false)
                            .setNegativeButton("Volver", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alert = builder.create();
                    alert.show();
                }


                if (base == null && altura == null){
                    AlertDialog.Builder builder = new AlertDialog.Builder(miActividad.getApplicationContext());
                    builder.setMessage("Todos los parámetros están vacíos")
                            .setCancelable(false)
                            .setNegativeButton("Volver", new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();
                                }
                            });
                    AlertDialog alert = builder.create();
                    alert.show();
                }
            }
        });
    }
}
String base = textoBase.getText().toString();
int numeroBase = Integer.parseInt(base);

String altura = textoAltura.getText().toString();
int numeroAltura = Integer.parseInt(altura);

您不能对空字符串执行 .parseInt。这当然会导致崩溃。您必须先检查空字符串。

解法:

在这一行之前int numeroBase = Integer.parseInt(base);写下:

if (base.contentEquals("")) {
    Toast.makeText(MainActivity.this, "Please enter a value in Base", Toast.LENGTH_SHORT).show;
else {
    int numeroBase = Integer.parseInt(base);
    ....
}

也对这一行做同样的事情:int numeroAltura = Integer.parseInt(altura);

就是这样。

对作为输入获取的字符串进行空检查,然后执行解析。

在你的情况下这样做:

            TextView textoBase = findViewById(R.id.areaBase);
            String base = textoBase.getText().toString().trim();
            if(!TextUtils.isEmpty(base))
               int numeroBase = Integer.parseInt(base);
            else 
              // Show some message like "Field is Mandatory"

            TextView textoAltura = findViewById(R.id.areaAltura);
            String altura = textoAltura.getText().toString().trim();
            if(!TextUtils.isEmpty(base))
               int numeroAltura = Integer.parseInt(altura);
            else 
              // Show some message like "Field is Mandatory"

试试这个

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);