java - 如何在黑莓的类中添加 addGlobalEventListener?

标签 java blackberry

我做了一个多入口点项目,其中 App2 设置为自动运行,App1 根据用户请求运行。 我正在尝试从 App2 接收到的 App1 调用全局事件。

public class App2 implements GlobalEventListener {
    static public int counter = 0;
    public static final long countId = 0x1251402f595f81a5L;
    public static final long eventId = 0xba4b84944bb7429eL;

    private App2() {
        Application.getApplication().addGlobalEventListener(this);
    }

    public static App2 waitForSingleton() {
        counter = 2; // Added the counter in Runtime store in a similar way as
        // added in eventOccured method
        // Deleted some unuseful code
    }

    public void eventOccurred(long guid, int data0, int data1, Object object0,
            Object object1) {
        if (guid == eventId) {
            callMethodOnOccuranceOfEvent();
        }
    }

    public void callMethodOnOccuranceOfEvent() {
        counter++;
        RuntimeStore store = RuntimeStore.getRuntimeStore();
        Object obj = store.get(countId);
        if (obj == null) {
            store.put(countId, new Integer(counter));
        } else {
            store.put(countId, new Integer(counter));
        }
    }
}

然后在其他类(class)我试过

public class App1 extends MainScreen {
public App1() {
}

protected void makeMenu(Menu menu, int instance) {
    super.makeMenu(menu, instance);
    menu.add(new MenuItem("Call", 20, 10) {
        public void run() {
            callMethodonclick();
        }
    });
}

public void callMethodonclick() {
    ApplicationManager.getApplicationManager().postGlobalEvent(App2.eventId);
    RuntimeStore store = RuntimeStore.getRuntimeStore();
    Integer c = (Integer) store.get(App2.countId);
    add(new RichTextField("Event Recived#counter#" + c));
}

如果我调用事件三次

Event Recived#counter#2
Event Recived#counter#2
Event Recived#counter#2

而预期的结果是

Event Recived#counter#3
Event Recived#counter#4
Event Recived#counter#5

我猜这表明 App2 的对象不是 null 但 eventOccurred 从未被调用。 输出清楚地表明 callMethodonclick 无法发布全局事件,即使在构造函数中添加了 globalEventListener

最佳答案

一定是这样的

if (obj == null) {
     store.put(countId, new Integer(counter));
} else {
     store.replace(countId, new Integer(counter));
}

store.put() 抛出 IllegalArgumentException,因为存储中有东西(请参阅 API 引用),但此异常由某个系统线程处理,该线程调用 eventOccured() 方法并且不显示有关此异常的任何信息。它是各种黑莓错误之一。

关于java - 如何在黑莓的类中添加 addGlobalEventListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3932030/

相关文章:

java内部类私有(private)构造函数,公共(public)成员

java - Android - Node.getNodeValue() 在存在 ' 时被截断

java - 基础模拟考试 SCJP

Jquerymobile 演示无法在 Blackberry curve 8520 上运行

c++ - 一个 webview 如何填满整个页面?

java - PushSDK发送推送到MDS

java - 如何让 Hibernate 为新对象从 1 开始版本列?

Java 使用中心类中的对象

java - 延长黑莓横幅字段的高度

http - 在没有提供 BES 和 APN 的情况下,RIM 联盟合作伙伴计划是黑莓应用程序的唯一选择吗?