2

我需要我的应用程序启动一个线程(在单击一个按钮后)来执行某些操作,获取一些变量,然后推送一个显示这些变量的屏幕。问题是我不能让我的屏幕等待那个线程。每次运行应用程序时,我都必须刷新新屏幕才能查看变量值。我应该如何处理?我使用了该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());
          }
       });
    }
}

我哪里做错了?

4

1 回答 1

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());
              }
          });
      }
}
于 2013-05-16T12:34:31.953 回答