Java 如何在blackberry的类中添加addGlobalEventListener?
我做了一个多入口点项目,其中App2设置为自动运行,App1根据用户请求运行。 我试图从App1调用一个全局事件,由App2接收Java 如何在blackberry的类中添加addGlobalEventListener?,java,blackberry,Java,Blackberry,我做了一个多入口点项目,其中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 =
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
,但eventoccurrent从未调用过。
输出清楚地表明,
callMethodonclick
无法发布全局事件,即使构造函数中添加了globalEventListener
。您没有更新RichTextField中的文本。只有当没有与App2.RTSID_MY_APP关联的runtimestorage对象时,才会将其添加到屏幕中
您的代码需要通过将RichTextField放入App1对象的字段中来保持其句柄。然后更新run()方法中的文本
我编辑了您的代码,在if语句中添加了大括号,这使代码更加清晰。一定是这样的
if (obj == null) {
store.put(countId, new Integer(counter));
} else {
store.replace(countId, new Integer(counter));
}
store.put()抛出IllegalArgumentException,因为存储中存在某些内容(请参见API参考),但此异常是由某个系统线程处理的,该线程调用eventOccursed()方法,但不显示有关此异常的任何内容。这是各种黑莓错误之一。waitForSingleton()是否曾被调用过?你能让你的代码成为你所面临问题的一个最小的例子吗?是的,它被设置为自动运行。嘿,thanx,谢谢你的帮助。明天你肯定会更新代码。希望这次编辑能帮助你解决我的问题,你的帮助对我来说将是非常巨大的。