Alertdialog when there is an exception

Asked

Viewed 19 times

0

Good afternoon, I would like to know how to make a Alertdialog to be shown to the user when there is an exception , on android.

1 answer

0


Basically create a try catch and insert your code into the try. Should you make an exception within the try, will fall into the catch and will display a AlertDialog. See an example below:

try {

    // Aqui você coloca o código que talvez venha dar uma exceção

} catch (Exception e){

    AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Exception");
    alertDialog.setMessage(e.toString());
    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    alertDialog.show();
}

Browser other questions tagged

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