有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在blackberry的类中添加addGlobalEventListener?

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

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));
        }
    }
}

然后在另一节课上我试着

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,而是eventOccessed从未被调用。 输出清楚地表明callMethodonclick无法发布全局事件,即使globalEventListener已添加到构造函数中


共 (0) 个答案