views:

31

answers:

2

I have a splash screen that implements MainScreen, and I am trying to use Timer to wait 3 seconds to push a new screen.

TimerTask splashTask = new TimerTask() {
    public void run() {
        UiApplication.getUiApplication().pushScreen(new HomeNavigationScreen());
    }
};

timer.schedule(splashTask, 3000);

However, it throws IllegalStateException error, I am not sure what I am doing wrong?

I am new to eclipse and java, I am not sure how to view stack trace.

Here's what I found:

ApplicationManagerImpl.processExited : process process switching to background: pid=260
java.lang.IllegalStateException: UI engine accessed without holding the event lock.
Timer died: Thread[Thread-310688768,5]
+3  A: 

Diagnosis based on the original question:

Your first step should be to read the documentation for Timer and TimerTask, which gives the following conditions for an IllegalStateException at schedule(...):

If the timer's task execution thread terminates unexpectedly, for example, because its stop method is invoked, any further attempt to schedule a task on the timer will result in an IllegalStateException, as if the timer's cancel method had been invoked.

Did you .stop() at some point? Do you ever call cancel()? Can you check to see if the task threw an exception which abruptly terminated the Timer thread? Are you using the same Timer for other TimerTasks? Do you trust them?

Try wrapping your TimerTask logic with a broad exception handler and have it report directly to see if there's an internal unexpected exception, which would cause the next call to schedule to fail as described.

Also, a nitpick but an important one: Best practice when asking a question that includes "Why did I get this exception?" is to include the full exception and stack trace output. You'll get much higher quality answers and you won't have to read this text again, and then have to re-edit your question, etc.

Guidance for your specific error:

Well, read the exception statement. It looks like you've upset the UI engine. So you have two problems:

  1. You've upset the timer thread by handing it a task that blows up with an uncaught exception. Good policy to wrap these things in a try/catch block, where the catch operation is at least to print or log a usable stack trace and re-throw the exception.

  2. You've upset the UI event logic, see below...

I don't know the blackberry UI, but presumably what you're trying to do needs to be done within the GUI event loop. This KB entry should help. Better yet, read the API documentation. You need to hold the GUI lock, much like in Swing, to call pushScreen(). One way to do this is to alter your code to call via invokeLater() or invokeAndWait().

Candidate Code

This is untested as I have never and don't plan to do any BlackBerry development, but it compiles in my mind against the published BlackBerry API, FWIW. Try something like one of these:

TimerTask splashTask = new TimerTask() 
{
  public void run() {
    final UiApplication uia = UiApplication.getUiApplication();
    final Object eventLock = uia.getEventLock();
    synchronized(eventLock) {
       uia.pushScreen(new HomeNavigationScreen());
    }
  }
};

timer.schedule(splashTask, 3000);

or, less likely to introduce synchronization issues and potential deadlocks:

TimerTask splashTask = new TimerTask() 
{
  public void run() {
    final UiApplication uia = UiApplication.getUiApplication();
    uia.invokeLater(new Runnable() { 
      public void run() {
        uia.pushScreen(new HomeNavigationScreen());
     });
    }
  }
};

timer.schedule(splashTask, 3000);
andersoj
I didn't call cancel(), or stop().. I only have 1 Timer and 1 TimerTask. I am confused how to view full exception and stack trace output..
tpae
If your device is keeping you from seeing the full exception report, your first step should be to figure out how to get ahold of that report. Presumably either it is logged somewhere automatically, or you could log it yourself (wrap the call to schedule in a try/catch, catch the illegal state exception, and call `printStackTrace()` on it, worst case.)
andersoj
@Michael Donohue: OOPS. Cut'n'paste malfunction. Here: http://stackoverflow.com/questions/538778/how-do-i-keep-a-caught-exception-from-reporting-as-uncaught-in-a-blackberry-app
andersoj
thank you! worked perfectly :)
tpae
+1  A: 

As Andersoj pointed out, you are trying to make a UI change without holding the UI event lock. invokeLater is a mechanism for getting code to execute and safely make UI changes.

TimerTask splashTask = new TimerTask() {
    public void run() {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                UiApplication.getUiApplication().pushScreen(new HomeNavigationScreen());
            }
        }
    }
};

timer.schedule(splashTask, 3000);
Michael Donohue