I’m trying to create a count down timer. I user a Timer object that is given a timertask. I have a label field called mTimerDisplay that is a static member of a class I call cGlobols.
On the run method, that gets called by the timer, can change the color of the label fine:
cGlobals.mTimerDisplay.setBackground(
BackgroundFactory.createSolidBackground(0xff0000));
When I add the following line:
cGlobals.mTimerDisplay.setText("Hi");
it throws a IllegalStateException:
I looked this up and it says
Signals that a method has been invoked
at an illegal or inappropriate time.
In other words, the Java environment
or Java application is not in an
appropriate state for the requested
operation.,
Does this mean I cannot do this operation on the thread, but if so why can I change the color? If I cannot do this on the timer thread, is there another way to do this?
A worker thread cannot update the UI without attaining the event thread. Wrap the setText() invocation in a synchronized block, and use Application.getEventLock() to attain this lock.