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.
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.
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();
}