对于如何在黑莓JDE中实现FieldChangeListener,我有点困惑。一种方法让我让我的主类实现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。
似乎所有的东西都连接起来了,但是我的print语句会显示出来,如果我调用了一个方法,我就会得到一个运行时错误。
这有道理吗?我是不是完全搞不懂如何在黑莓上使用听众?
http://pastie.org/618950
我的代码作为一个整体.
发布于 2009-09-18 16:12:18
我很困惑,但我设法解决了问题。我从头开始创建了一个新类,然后复制并粘贴了我的旧代码。一切都正常。我所做的唯一改变就是导入Eclipse说是必要的类(在我从各种教程获得一些导入语句之前,等等,因此有些类可能没有被使用)。
有可能是我进口的东西会导致物体崩溃吗?
我真的希望我的大部分代码都在屏幕上,但在加载之前,我会尝试让整个代码崩溃。关于我正在使用的xml解析器--不高兴。
http://pastie.org/621932
这是修改过的代码。我真的很沮丧,因为我知道对这个框架工作有一些内在的理解,而我并不是在摸索,而且我的大部分烦恼都来自于这个框架。我想只有练习才能帮助我,尽管^_^;
发布于 2009-09-16 17:57:24
我看了你的代码,没有什么明显的错误从我身上跳出来。但是,我不会指定主应用程序类作为FieldChangeListener的职责。这不是它应该知道的事情。我能为您做的最好的就是提供一个示例应用程序,该应用程序为ButtonField实现了ButtonField接口。这不是一个解决方案,但是如果您对代码有了更好的了解,您将能够挑选出与本示例不同的内容。希望能帮上忙。
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();
}
}
}发布于 2009-09-17 08:56:41
我同意Fostah(+1)的观点,在Field、Manager或Screen中实现FieldChangeListener或使用独立的FieldChangeListener是常见的。
此外,要从字段推拉屏幕:
UiApplication.getUiApplication().pushScreen(nextScreen);请参阅How to navigate back to the previous screen in the Blackberry emulator?
https://stackoverflow.com/questions/1433945
复制相似问题