2012-12-26 43 views
1

使用定时器我想以这种方式来使用定时器在我的黑莓手机项目 -黑莓项目

Timer timer = new Timer(); 
timer.schedule(new TimerTask() { 
    public void run() { 
     pushScreen(new MyScreen()); 
    } 
},200); 

,但在执行程序时,我得到一个运行时异常。 有人可以告诉我这段代码有什么问题吗?或者在BlackBerry项目中使用Timer的任何其他技巧。

我的目标是推动SplashScreen 10秒,然后MyScreen页面打开。所以我想在打开“自选画面”页面时使用计时器10秒延迟,在计时器期间,我将显示SplashScreen页面。

+1

你可以发布堆栈跟踪吗? –

+0

哪个操作系统版本? –

+0

我正在使用Windows 7中的“BlackBerry Java sdk 7.1”。 –

回答

2

由于Richard mentioned in his answer,你有问题,因为你试图从主线(又名“UI”)线程以外的线程操纵UI。你只需要一个小的变化,使你的代码正常工作:

UiApplication.getUiApplication().invokeLater(new Runnable() { 
               public void run() { 
                pushScreen(new MyScreen()); 
               } 
              }, 
              200 /* delay */, 
              false /* repeat = no */); 

以上为您发布,黑莓的Java代码的等价物。

我的目标是推动SplashScreen 10秒,然后MyScreen页面将打开 。所以我想使用定时器10秒延迟,同时打开 自选画面页面,在计时器期间,我将显示SplashScreen 页面。

如果这种情况真的是你想做的事,那么就使你的SplashScreen只要应用启动会出现什么:

public class MyApp extends UiApplication 
{ 
    /** 
    * Entry point for application 
    * @param args Command line arguments (not used) 
    */ 
    public static void main(String[] args) 
    { 
     // Create a new instance of the application and make the currently 
     // running thread the application's event dispatch thread. 
     MyApp theApp = new MyApp();  
     theApp.enterEventDispatcher(); 
    } 

    public MyApp() 
    {   
     // Push a screen onto the UI stack for rendering. 
     final SplashScreen splashScreen = new SplashScreen(); 
     pushScreen(splashScreen); 

     UiApplication.getUiApplication().invokeLater(new Runnable() { 
                 public void run() { 
                  pushScreen(new MyScreen()); 
                  popScreen(splashScreen); 
                 } 
                }, 
                10*1000 /* delay in msec */, 
                false /* repeat = no */); 

    } 

这确实你问什么,但链接,理查德还提供允许用户尽早关闭启动画面。这可能是也可能不是你想要的,所以我只是提供上面的替代方案。

-2

对于Android的你可能会想要做这样的事情:

initialize(); 
setButtonListeners(); 
new Thread() { 
    public void run() { 
     try { 
      sleep(3000); 
     } catch (Exception e) { 
     } finally { 
       Intent menuIntent = new Intent(SplashLoadScreen.this, 
         MainMenu.class); 
       startActivity(menuIntent); 
     } 
    } 
}.start(); 

我不是太熟悉的黑莓手机,但似乎你使用pushScreen()而不是startActivity(),而你不知道“T使用意图像Android这样做,所以也许事情,因为这:

initialize(); //Method to initialize all variables you might want to use. 
//...Some code 
new Thread() { 
    public void run() { 
     try { 
      sleep(3000); //Time in milliseconds 
      //to make this thread sleep before executing whatever other code. 
     } catch (Exception e) { 
     } finally { 
       pushScreen(new MyScreen()); //Push next screen 
     } 
    } 
}.start(); 

在try {}赶上(){}最后{}就是异常处理。 基本上,如果在试图睡眠3000毫秒时发生任何错误,那么它将捕获所有异常(也称为错误),并执行catch(){}中的任何操作。然后,在try {}(如果没有发现异常)或catch(){}(如果发现错误)完成之后,它会执行finally {}中的任何操作。这个案例最终将推动下一个屏幕。

+0

@ chriswins2much- thanx但我只在BlackBerry中遇到问题,而且您的代码也不适用于BlackBerry。 –

+0

BlackBerry用什么语言编写的? – chriswins2much

+0

@ chriswins2much-在Java中 –

1

很难说究竟到底发生了什么问题,但有一件事你不应该在非事件线程的线程上与用户界面交互。

它不会教你如何使用计时器,但有一个developer article如何做一个启动画面。

0

您每200毫秒推一个新的屏幕... 您需要在推送屏幕时终止计时器。请记住,时间间隔以毫秒为单位,因此您需要计算该时间间隔。

祝你好运!