domingo, 4 de abril de 2010

Exception at TimerTask.run()java.lang.RuntimeException: pushModalScreen called by a non-event thread

Tratando de abrir una ventana de diálogo desde un thread en una aplicación para Blackberry, fallaba con:
Exception at TimerTask.run()java.lang.RuntimeException: pushModalScreen called by a non-event thread

El código que lo provocaba era:

public class RemindTask extends TimerTask {
public void run() {
try {
currentTime = currentTime - 1;
System.out.println("currentTime = " + currentTime);
countdown.setDate(currentTime);
if (currentTime <= 0) {
oneTimer.cancel();
Dialog.alert("Countdown completed");
}
} catch (Exception e) {
System.out.println("Exception at TimerTask.run()" + e.toString());
e.printStackTrace();
}
}
}


La solución es incorporar esa acción dentro del thread de Eventos, de esta forma:
public class RemindTask extends TimerTask {
public void run() {
try {
currentTime = currentTime - 1;
System.out.println("currentTime = " + currentTime);
countdown.setDate(currentTime);
if (currentTime <= 0) {
oneTimer.cancel();

UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("Countdown completed");
}
});

}
} catch (Exception e) {
System.out.println("Exception at TimerTask.run()" + e.toString());
e.printStackTrace();
}
}
}

La solución la obtuve del foro java.lang.RuntimeException:pushModalScreen called by a non-event thread

Tambien se pueden consultar estas referencias

http://www.blackberry.com/developers/docs/5.0.0api/UI-summary.html

http://www.thinkingblackberry.com/archives/182

No hay comentarios: