Implementing the error screen

The ErrorScreen class creates an Alert of type ERROR, which is shown in situations where, for example, no contacts lists are found. The exact message String is entered in the showError method, which the EventSharingMIDlet calls when an exception is caught.

To implement the ErrorScreen class:

  1. Create the ErrorScreen.java class file.

  2. Import the required packages, and initialize the required parameters.

    import javax.microedition.lcdui.*;
    import javax.microedition.pim.*;
    
    class ErrorScreen extends Alert {
        private static Image image;
        private static Display display;
        private static ErrorScreen instance = null;
  3. Define the Alert properties and create a method for displaying it.

        private ErrorScreen() {
            super("Error");
            setType(AlertType.ERROR);
            setTimeout(5000);
            setImage(image);
        }
    
        static void init(Image img, Display disp) {
            image = img;
            display = disp;
        }
    
        static void showError(String message, Displayable next) {
            if (instance == null) {
                instance = new ErrorScreen();
            }
            if(message.indexOf("Sending event ")!=-1 || message.indexOf("Event inserted in the local database")!=1)
                instance.setTitle("Info");
            else
                instance.setTitle("Error");
            instance.setString(message);
            display.setCurrent(instance, next);
        }
    
        static void reportPIMException(PIMException e, Displayable next) {
            StringBuffer errorMsg = new StringBuffer("Error:");
            switch (e.getReason()) {
            case PIMException.FEATURE_NOT_SUPPORTED:
                errorMsg.append("The requested feature is not supported");
                break;
            default:
                errorMsg.append("Unknown error");
            }
            showError(errorMsg.toString(), next);
        }
    }