我需要我的应用程序启动一个线程(点击一个按钮后)做某事,给我一些变量然后按一个显示这些变量的屏幕。问题是我无法让我的屏幕等待该线程。每次运行应用程序时,我都必须刷新新屏幕以查看变量值。我应该怎么处理?我使用invokeLater()
方法,但似乎不起作用!
这是我的第一个屏幕代码:
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
public class MyScreen extends MainScreen implements FieldChangeListener
{
public MyScreen()
{
super();
setTitle("MyScreen!");
ButtonField bf = new ButtonField("Start thread", ButtonField.CONSUME_CLICK) ;
this.add(bf);
bf.setChangeListener(this);
}
public void fieldChanged(Field Field, int context) {
MyThreadClass myThread = new MyThreadClass();
myThread.start();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(new NewScreen());
}
});
}
}
我哪里出错了?
答案 0 :(得分:2)
试试这个 -
MyThreadClass myThread = new MyThreadClass();
myThread.start();
和
class MyThreadClass implements Runnable {
public void run() {
// here - your code for getting variables.
// After getting your variable , push the screen.
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(new NewScreen());
}
});
}
}