2012-04-29 58 views
-3

我想再次显示对话框,而我从Web服务 我使用LWUIT加载一些数据,多线程在J2ME和LWUIT

以下是代码

public class LoaderAnimation extends Container implements Runnable { 

private Thread t; 
private boolean running = false; 

public LoaderAnimation() { 
} 

public void start() { 
    running = true; 
    t = new Thread(this); 
    t.start(); 
} 

public void run() { 
    while (running) { 

      // do something 
      t.sleep(150); 
    } 
} 

public void stop() { 
    running = false; 
} 
} 

现在发生的事情,它运行,但调用Web服务的代码中有停止工作

那就是它的调用

public static void showLoaderScreen() 
    { 
     dialog = new Dialog(); 
     dialog.setLayout(new BorderLayout()); 
     canvas = new LoaderAnimation(); 
     dialog.addComponent(BorderLayout.CENTER , canvas); 
     canvas.start(); 
     dialog.show(); 
    } 

public static void dismissLoaderScreen() 
{ 
    canvas.stop(); 
    dialog.dispose(); 
} 
+1

并代码段你贴编译?据我所知,[t.sleep(150)](http://docs.oracle.com/javame/config/cldc/ref-impl/midp2.0/jsr118/java/lang/Thread.html#睡眠(long)“API javadocs”)会抛出'InterruptedException',但没有捕获 - 并且你肯定无法重新抛出,因为它是'Runnable'。另外,你最好将运行声明为volatile – gnat 2012-04-29 14:45:32

回答

1

试试这段代码。

private void startLoader() { 
    Dialog d = new Dialog(); 
    d.getStyle().setBgColor(0xffffff); 
    d.getStyle().setBgTransparency(255); 
    d.show(100, 250, 90, 150, true, false); 
    d.setAutoDispose(true); 
    try { 
     Thread.sleep(30); 
    } catch (InterruptedException ex) { 
     ex.printStackTrace(); 
    } 
    d.dispose(); 
    new Timer().schedule(new TimerTask() { 

     public void run() { 

      new Loader().start(); 
     } 
    }, 30); 
} 

Loader类我们可以写的东西解析或Web服务处理等

class Loader extends Thread 

{  public void run() { 

      try { 

       ServiceTypesScreen st = new ServiceTypesScreen(); 
       st.init(); 
      } catch (Exception e) {      
       e.printStackTrace(); 
      } 

     } 
}