AlertDialog when there is an exception

0

Good afternoon, I'd like to know how I get an AlertDialog to be shown to the user when there's an exception on the android.

    
asked by anonymous 01.07.2017 / 21:02

1 answer

0

Basically create a try catch and enter your code within try . If you give any exceptions within try , it will fall into catch and display AlertDialog . Here's 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();
}
    
01.07.2017 / 21:14