Я боролся с этой проблемой, которая продолжает сбой моего приложения каждый раз, когда в нем есть пустое поле.
, когда я запускаю код, он говорит мне, что в строках есть ошибка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();
}
}
});
}
}