黑莓JDE FieldChangeListener
我对如何在Blackberry JDE中实现FieldChangeListener感到困惑。一种方法有我做我的主类实现FieldChangeListener,再有它内部的一个fieldchanged方法,而另一个有我做的事:黑莓JDE FieldChangeListener
FieldChangeListener listenerUS = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Something changed!");
pushScreen(_newScreen);
}
};
无论哪种方式,如果我尝试调用一个方法(如pushScreen,或自定义方法我写了),我得到一个运行时错误。在调试模式下,我的打印语句也没有显示出来。但是,如果我彻底删除fieldChanged方法,它甚至不会编译,所以我确定它看到代码?
我已经将侦听器添加到我希望它连接到的按钮具有:
but_temp.setChangeListener(this);
(在第一种情况下),或者通过将listenerUS
似乎一切都勾搭上了,但我的打印报表显示了,如果我调用一个方法,我收到了。运行时错误。
这有意义吗?我对如何在黑莓手机上使用听众完全困惑?
有整体的我的代码的副本...
我超级困惑,但我设法解决的事情。我从头开始创建一个新类,然后复制并粘贴旧代码。一切正常。我改变的唯一的事情是只导入Eclipse所说的必需的类(在我从各种教程等得到一些导入语句之前,所以有些可能没有被使用)。
是否有可能我导入了导致事情要崩溃?
我真的宁愿让我的大部分代码都在屏幕本身,但试图在我甚至可以加载之前崩溃整个事情。关于我使用的xml解析器的一些事情并不开心。
还有修改后的代码。我非常沮丧,因为我知道我对这个框架工作有一些固有的理解,我不是很热心,而且我的大部分麻烦都来自于此。我认为只有练习才能帮助我,尽管^ _^;;
我看着你的代码并没有什么错公然跳出我。但是,我不会指定主应用程序类是FieldChangeListener的职责。这不是应该注意的事情。我能为你做的最好的事情就是提供一个为ButtonField实现FieldChangeListener接口的示例应用程序。这不是一个解决方案,但也许随着你对代码更好的了解,你将能够挑选出与本例不同的东西。希望能帮助到你。
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.FieldChangeListener;
/**
* Test implementation of ButtonField.
*/
public class TestAppMain extends UiApplication
{
/**
* Default Constructor.
*/
private TestAppMain() {
pushScreen(new AppScreen());
}
/**
* App entry point.
* @param args Arguments.
*/
public static void main(String[] args) {
TestAppMain app = new TestAppMain();
app.enterEventDispatcher();
}
/**
* Main application screen.
*/
private static class AppScreen extends MainScreen
{
/**
* Default constructor.
*/
public AppScreen() {
LabelField title = new LabelField("Button Test Demo",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);
// Create a button with a field change listener.
FieldChangeListener listener = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
ButtonField buttonField = (ButtonField) field;
System.out.println("Button pressed: " + buttonField.getLabel());
}
};
ButtonField buttonField = new ButtonField("Test Button", ButtonField.CONSUME_CLICK);
buttonField.setChangeListener(listener);
add(buttonField);
}
/**
* Handle app closing.
*/
public void close() {
Dialog.alert("Goodbye!");
System.exit(0);
super.close();
}
}
}
*点头*是的,这是我做的另一种方式。但是,我很困惑,屏幕本身有多少,主要代码中的方法。很多BB提供的教程都将屏幕大部分保留为空白。不过,在屏幕上做事情确实很有意义。 – Jenny 2009-09-17 14:27:28
我Fostah(+1)同意,这是常见的实现FieldChangeListener在现场,经理或屏幕,或者用一个独立的FieldChangeListener。
另外,从现场推/拉幕:
UiApplication.getUiApplication().pushScreen(nextScreen);
见How to navigate back to the previous screen in the Blackberry emulator?
Hrrm ...试图做到这一点让我打印语句成功地打印(万岁!),但该程序崩溃更难。之前,去了白错误屏幕的“” JUM错误104:未捕获的空指针异常”,但没有实际上我赶出去(我可以打‘继续’,并保持运行我的程序然而,现在它踢我进入在Eclipse中调试角度,如果我解开我的代码更改拿回来是怎么回事,它仍然这样做,这让我困惑。 – Jenny 2009-09-17 14:56:39
调用监听器内的任何方法,它崩溃的调试屏幕“Screen.class”还有,我想我会重构所有代码,使工作的冲击发生在我providerScreen类,然后看看会发生什么。 – Jenny 2009-09-17 15:11:36
当你找到原因和解决方案,这将是巨大的,有你的答案。 – 2009-09-17 17:12:23
我在所发生的事情感到困惑。你说println()调用没有发生,但同时你说pushScreen()会导致运行时错误?另外,什么是运行时错误? – Fostah 2009-09-16 17:14:48
之前,当我写这个时,它是一个“JUM错误104:未捕获的空指针异常”。 NOW它只是在eclipse中的调试屏幕上崩溃,即使代码没有改变。此外,由于某种原因,它现在正在我的印刷版上。 – Jenny 2009-09-17 15:14:02