2010-11-15 28 views
0

行,所以我得到这个设置: 一个MIDletJ2ME的MIDlet双线程未能回调

Gui extends Midlet{ 
     private static Gui instance; 

     protected void startApp() { 
      Display.getDisplay(this).setCurrent(MyForm.getInstance()); 
     } 

      private static final Logger log = LoggerFactory.getLogger(); 

     public static Datacheck getInstance() { 
      return instance; 
     } 

     public Gui() { 
      // Configure logging 
     } 

     protected void startApp() { 
      instance = this; 
      Display.getDisplay(this).setCurrent(MyForm.getInstance()); 
     } 

     protected void pauseApp() { 
     } 

     protected void destroyApp(boolean bool) { 
      // Shutdown Microlog gracefully 
      LoggerFactory.shutdown(); 
      notifyDestroyed(); 
     } 

     public static void log(Level level, String message) { 
      log.log(level, message); 
     } 

     public void requestScreen(Form screen) { 
      log.info("request screen called"); 
      Display.getDisplay(this).setCurrent(screen); 
     } 
    } 

形式

MyForm extends Form{ 
     private static MyForm instance; 

     public static MyForm getInstance() { 
      if (instance == null) { 
       instance = new MyForm(); 
      } 
      return instance; 
     } 

     private Form(){ 
      //start task 
      new Timer().scheduleAtFixedRate(new PollingService(CallsDialog.getInstance()), 0, UPDATE_INTERVAL); 

      //add gui elements .... 
     } 

     private void updateForm() { 
      //never gets executed 
     } 
    } 

和线程

MyThread implements Runnable{ 
     private MyForm handle; 

     public PollingService(MyForm handle) { 
      this.handle = handle; 
     } 

     public void run() { 
      handle.updateForm(); 
     } 
    } 

所以该MIDlet启动,将其表单设置为MyForm的实例 然后myform创建一个新线程 这个线程应该调用myForm会的功能每5秒

这是真实的东西非常简化的例子,所以请,当我从执行的方法“不改变线程设计

现在MyForm“类 它不执行 我没有得到任何错误

任何人都知道我在做什么错?

编辑 改变,所以没有创建的线程(已被TimerTask的完成)

+0

尝试把SOP和打印stacktrace在catch中,尝试调试,代码似乎没问题。 – 2010-11-15 07:00:48

回答

1

1)你不需要创建单独的线程来执行TimerTask的。 Timer和TimerTask机制已经包含为每个TimerTask执行创建新线程。

2)你能提供更实际的代码吗?在你的例子中没有创建MyThread,也没有启动()调用。有时候错误只是缺少方法调用。

+0

thx回复,问题是UpdateForm看起来像这样:updateGui(element,checkIfSoundNotifyIsNeeded());现在checkIFSoundNotifyIsNeeded给出了一个异常(空指针),我将能够修复其余的:),虽然你没有得到任何错误的stange:s – Berty 2010-11-20 22:12:54