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 TimerTask
s? 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:
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.
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);