Ted pottel
Ted pottel

Reputation: 6983

blackberry trying to create a count down timer and exception gets thrown when text is updated

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?

Upvotes: 0

Views: 278

Answers (1)

1in9ui5t
1in9ui5t

Reputation: 126

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.

Upvotes: 3

Related Questions