Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
beenjaminnn
Posts: 49
Registered: ‎05-02-2012
My Device: Bold 9790

Re: Timer that starts right away

[ Edited ]

http://www.blackberry.com/developers/docs/4.1api/java/lang/IllegalStateException.html

 

When would the application not be in a suitable state to setText?

 

Right now I have

 

TimerTask autoUpdate = new TimerTask() {
    public void run() {
        Date now = new Date();
        try {
            Field1.setText(now.toString());
        }
        catch(Exception ex) {
            Field1.setText("Error");
        }
    }
};

 

It shows "Error" for one minute and then seems to work for a while, then it gives ArrayOutOfBounds exception? I think that it throws the array exception because everything was working tried and true before

 

Thanks,

Ben

Developer
peter_strange
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: Timer that starts right away

As I said in one of my previous posts:

"In your run() method, you will have to use invokeLater to update the screen".

 

If you don't, you will get an IllegalStateException.

 

To understand this, you need to read up on the Event Thread.

 

Start here:

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

 

this is also useful:

http://supportforums.blackberry.com/t5/Java-Development/Update-a-screen-on-the-Main-Event-Thread/ta-...

 

Note that the comment about using invalidate() is not really correct, there are only a few circumstances where you will need to use invalidate(). 

Contributor
beenjaminnn
Posts: 49
Registered: ‎05-02-2012
My Device: Bold 9790

Re: Timer that starts right away

Alright got it! So just do double check, I have

 

Timer t = new Timer();

 

TimerTask autoUpdate = new TimerTask() {
    public void run() {
        UiApplication.getUiApplication().invokeLater (new Runnable() {
            public void run()
            {
                //Update
            }
        });
        
    }
};

 

protected void onUiEngineAttached(boolean attached) {
    if(attached) {
        t.scheduleAtFixedRate(autoUpdate, 0, 60000);
    }
    else {
        t.cancel();
    }
}

 

And it works! Thank you so much.

 

Ben