我正在尝试以这种方式在我的 BlackBerry 项目中使用计时器 -
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
pushScreen(new MyScreen());
}
},200);
但是我在执行程序时遇到运行时异常。 有人可以告诉我这段代码有什么问题吗?或在 BlackBerry 项目中使用计时器的任何其他技巧。
我的目标是按下 SplashScreen 10 秒,然后 MyScreen 页面将打开。因此,我想在打开 MyScreen 页面时使用计时器延迟 10 秒,并在计时器期间显示 SplashScreen 页面。
最佳答案
如Richard mentioned in his answer ,您遇到问题是因为您尝试从主(也称为“UI”)线程以外的线程操作 UI。您只需要进行一些小的更改即可使您的代码正常工作:
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
pushScreen(new MyScreen());
}
},
200 /* delay */,
false /* repeat = no */);
上面的代码相当于您发布的 BlackBerry Java 代码。
My goal is to push SplashScreen for 10 sec and then MyScreen page will be open. So I want to use timer for a 10 second delay while opening the MyScreen page and during the timer I will display the SplashScreen page.
如果这确实是您想要做的,那么只需让您的 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 */);
}
这符合您的要求,但理查德提供的链接还允许用户提前关闭启动屏幕。这可能是也可能不是您想要的,所以我只是提供上面的替代方案。
关于java - BlackBerry项目中Timer的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14034971/