【发布时间】:2013-09-06 21:17:07
【问题描述】:
我正在尝试让我的 Blackberry 应用程序显示一个自定义模式对话框,并让打开线程等到用户关闭对话框屏幕。
final Screen dialog = new FullScreen();
...// Fields are added to dialog
Application.getApplication().invokeAndWait(new Runnable()
{
public void run()
{
Application.getUiApplication().pushModalScreen(dialog);
}
});
尽管我使用 invokeAndWait 从事件线程调用 pushModalScreen,但会抛出一个异常,上面写着“非事件线程调用 pushModalScreen”。
对真正的问题有什么想法吗?
这里是复制这个问题的代码:
package com.test;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
public class Application extends UiApplication {
public static void main(String[] args)
{
new Application();
}
private Application()
{
new Thread()
{
public void run()
{
Application.this.enterEventDispatcher();
}
}.start();
final Screen dialog = new FullScreen();
final ButtonField closeButton = new ButtonField("Close Dialog");
closeButton.setChangeListener(new FieldChangeListener()
{
public void fieldChanged(Field field, int context)
{
Application.getUiApplication().popScreen(dialog);
}
});
dialog.add(closeButton);
Application.getApplication().invokeAndWait(new Runnable()
{
public void run()
{
try
{
Application.getUiApplication().pushModalScreen(dialog);
}
catch (Exception e)
{
// To see the Exception in the debugger
throw new RuntimeException(e.getMessage());
}
}
});
System.exit(0);
}
}
我使用的是组件包版本 4.5.0。
【问题讨论】:
-
这是 UIApplication 还是后台应用程序?
-
您是否在应用程序中将其与某些系统侦听器(例如 PhoneListener 或 SendListener)一起使用?
-
这是一个 UI 应用。我已经添加了演示项目的代码来说明问题的问题。
-
我相信 Bradley 的回答指向了问题的核心,即 enterEventDispatcher() 的调用没有按顺序在调用 invokeAndWait 之前运行,因此可能还没有可用的 UI 线程。