Application does not display Alertdialog

Asked

Viewed 14 times

1

Hello!

I’m developing an app in Android Studio that at a certain point instantiates an object called Mesa with user input. For this, there is a button with the following behavior:

if (botao_criar_mesa.isEnabled()) {
        botao_criar_mesa.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Mesa novaMesa = helper.criaNovaMesa();
            }
        });
    }

The helper in the above code is a class that does a check on incoming inputs. If you do not obey a rule, the app must display a Alertdialog, and this is not happening. Follow code from the helper:

public AcertoDeContasHelper(AcertoDeContasActivity activity) {
    campoNumeroPessoas = activity.findViewById(R.id.nPessoas);
    campoValorMesa = activity.findViewById(R.id.valorMesa);
    alerta = new AlertDialog.Builder(activity).create();
    alerta.setTitle("Oops... temos um erro!");
    alerta.setButton(AlertDialog.BUTTON_POSITIVE, "Entendi!", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which){
            alerta.dismiss();
        }
    });
    alerta.setButton(AlertDialog.BUTTON_NEGATIVE, "Quero ajuda", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //TODO: implementar intent para a tela de ajuda
        }
    });
}

public Mesa criaNovaMesa() {
    Mesa mesa = new Mesa();
    int numeroPessoas = Integer.parseInt(campoNumeroPessoas.getText().toString());
    BigDecimal valorMesa = new BigDecimal(campoValorMesa.getText().toString());
    validaValores(mesa, numeroPessoas, valorMesa);
    return mesa;
}

private void validaValores(Mesa mesa, int numeroPessoas, BigDecimal valorMesa){
    if (numeroPessoas > 0 && numeroPessoas <= 30) {
        mesa.setNumeroPessoas(numeroPessoas);
    } else if (numeroPessoas <= 0 ) {
        alerta.setMessage("O número de pessoas deve ser no mínimo 1!");
        alerta.show();
    } else {
        alerta.setMessage("O número de pessoas deve ser no máximo 30!");
        alerta.show();
    }
}

Does anyone know what the mistake is and what should I do to correct it? I will be here to clear up any doubts.

Thanks in advance!

  • Mate, I think without testing it’s complicated. A tip I give you is to debug and check if the conditions to access the alert.show command are being true at the time due.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.