Alert after Alert in J2ME

I just discovered a very nasty restriction in J2ME concerning Alerts whilst developing a MIDP 2.0 application for a Nokia E70 Mobile Phone (running Symbian 60 3rd Edition). As has become common now, the code I had worked find in the Sun Wireless Toolkit emulator but not so on the E70. Anyway, here’s the deal …

If you display an Alert object (e.g. a Yes/No dialog), and then (as a result of the user selecting Yes or No) try to display another Alert object after that, the phone will generate an IllegalArgument exception. This is because of the default behaviour of the Display.setCurrent(Displayable) method which assumes that you want to return to the object that created the Alert after the new alert is dismissed. Now, in my view, this is a reaonable thing to want to do. However, the exception that is thrown suggests that an “Alert cannot return to an Alert” which is not documented anywhere that I’ve seen.

So, the solution was to use the Display.setCurrent(Alert,Displayable) method and ensure that the second parameter is never another Alert.

Thanks to Naomi and Brian for their help on this.


3 thoughts on “Alert after Alert in J2ME”

  1. Hi there!

    Thanks for this posting in your blog – i just had the same problem with my own J2ME application (GPSTron) Because of your posting i was able to fix that problem in about 5 Minutes 🙂

    Kind regards

    Hendrik Bock

  2. Hi, thanks for acknowledging this issue, I have been pulling my hair out for about an hour over this one!

    My workaround to this problem was to create the following simple canvas, and have that adjust the display when it is first painted.

    private static class BlackCanvas extends Canvas {
    private Display display;
    private Alert next;

    public BlackCanvas(Display d, Alert a) {
    display = d;
    next = a;
    }

    protected void paint(Graphics g) {
    g.setColor(0x000000);
    g.fillRect(0, 0, getWidth(), getHeight());

    display.setCurrent(next);
    }
    }

    And then,

    public void switchDisplay(Alert a) {
    if (display.getCurrent() instanceof Alert)
    display.setCurrent(new BlackCanvas(display, a));
    else
    display.setCurrent(a);
    }

Leave a Reply