Code to create generic alert dialog to display error messages in Android

Activity Forums Android mobile app Code to create generic alert dialog to display error messages in Android

  • This topic is empty.
  • Creator
    Topic
  • #126
    adcocitclass
    Keymaster

    While error handling in Android java, use below code as reference to create generic alert dialog to display required error messages. Below is Android Java code,

    private void doStuff()
    {
    try
    {
    //do some stuff here
    }
    catch(Exception e)
    {
    messageBox("doStuff", e.getMessage());
    }
    }

    //*********************************************************
    //generic dialog, takes in the method name and error message
    //*********************************************************
    private void messageBox(String method, String message)
    {
    Log.d("EXCEPTION: " + method, message);

    AlertDialog.Builder messageBox = new AlertDialog.Builder(this);
    messageBox.setTitle(method);
    messageBox.setMessage(message);
    messageBox.setCancelable(false);
    messageBox.setNeutralButton("OK", null);
    messageBox.show();
    }

  • You must be logged in to reply to this topic.