The problem is that the event thread is the only thread allowed to modify the UI.
There are a couple of ways to do this
1) pass a Runnable to the event thread, with the code that modifies the UI, or
2) obtain the event lock.
Method #1 is more foolproof. Here is how you do it:
UiApplication.getUiApplication().invokeAndWait(new Runnable()
{
public void run() {
//code that modifies UI goes here.
}
});
invokeAndWait will block, there is also an async version - invokeLater
Much more detail here.