Blackberry Java中的类间调用

Blackberry Java中的类间调用,java,eclipse,blackberry,Java,Eclipse,Blackberry,当屏幕上的位图被“点击”时,我试图推送一个新屏幕。为此,我在此帖子中创建了一个类:我在下面发布了它的部分代码: public class CustomMenuButtonField extends Field{ Bitmap normal,focused; ... protected boolean navigationClick(int status, int time) { // push new screen fieldC

当屏幕上的位图被“点击”时,我试图推送一个新屏幕。为此,我在此帖子中创建了一个类:我在下面发布了它的部分代码:

public class CustomMenuButtonField extends Field{
    Bitmap normal,focused;

    ...

    protected boolean navigationClick(int status, int time)
    {
        // push new screen
        fieldChangeNotify(0);
        return true;
    }

    ...
我想在用户单击位图时推送一个新屏幕。我曾参考过这个帖子: ,但我仍然无法找到调用新屏幕命令的命令。到目前为止,我拥有的用户界面是:

public class UserInterface extends UiApplication {
    public static void main(String[] args){
        UserInterface theApp = new UserInterface();
        theApp.enterEventDispatcher();
    }
    public UserInterface() {
        pushScreen(new UserInterfaceScreen());
    }
}

final class UserInterfaceScreen extends MainScreen {
    public UserInterfaceScreen() {
    ...
弹出新屏幕的命令是什么?更重要的是,我可以在哪里操作它?我知道它可能应该使用pushScreen(),但这在该类中是无法识别的。我是否要从单击扩展主屏幕创建一个新的最终类新闻屏幕?如果是这样,我将如何调用它并将其放入eventDispatcher。我一直在浏览blackberry网站,但是他们没有太多关于这个问题的示例代码,而且我对Java非常陌生,所以这让我相当困惑

您可以使用:

UiApplication.getUiApplication().pushScreen(new HomeScreen());
如果应用程序没有执行任何奇怪的操作,UiApplication.getUiApplication()将始终返回程序的UiApplication对象。这是为每个UiApplication上下文创建的单例对象

您还可以使用:

UserInterface.getUiApplication().pushScreen(new HomeScreen());
如果UserInterface类对您正在处理的类可见,但这会降低代码的可重用性


我在我的博客上收集了一些基本的样本。看看这个。从底部开始,一路向上。

试试下面我添加的UserInterfaceScreen代码。构造UserInterfaceScreen时,它会添加一个CustomMenuButtonField并设置一个字段更改侦听器。单击按钮时,它会将新屏幕推到堆栈中

package mypackage;

import net.rim.device.api.ui.UiApplication;

public class UserInterface extends UiApplication {
public static void main(String[] args){
    UserInterface theApp = new UserInterface();
    theApp.enterEventDispatcher();
}
public UserInterface() {
    pushScreen(new UserInterfaceScreen());
}
}
这是UserInterfaceScreen

package mypackage;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;

public class UserInterfaceScreen extends MainScreen {

public UserInterfaceScreen() {
    super();
    //replace null with the image string
    CustomMenuButtonField button = new CustomMenuButtonField(null, null);
    button.setChangeListener(new FieldChangeListener() {

        public void fieldChanged(Field field, int context) {
            UiApplication.getUiApplication().pushScreen(new         MyHomeScreen());

        }
    });
}
}

请查看我发布的以下链接(alishaik786)中的答案:

//imagefield类如下所示

package com.pl.button;

import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;

public class imagefield extends Field {

private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;

private Bitmap _currentPicture;
private Bitmap _onPicture; 
private Bitmap _offPicture; 
int color;

public imagefield (String text, long style ,String img, String img_hvr, int color){
    super(style);


    _offPicture = Bitmap.getBitmapResource(img);
    _onPicture = Bitmap.getBitmapResource(img_hvr);

    _font = getFont();
    _label = text;
    _labelHeight = _onPicture.getHeight();  
    _labelWidth = _onPicture.getWidth();

    this.color = color;

    _currentPicture = _offPicture;
}

/**
 * @return The text on the button
 */
String getText(){
    return _label;
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#getPreferredHeight()
 */
public int getPreferredHeight(){
    return _labelHeight;
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#getPreferredWidth()
 */
public int getPreferredWidth(){
    return _labelWidth;
}

/**
 * Field implementation.  Changes the picture when focus is gained.
 * @see net.rim.device.api.ui.Field#onFocus(int)
 */
protected void onFocus(int direction) {
    _currentPicture = _onPicture;
    invalidate();
}

/**
 * Field implementation.  Changes picture back when focus is lost.
 * @see net.rim.device.api.ui.Field#onUnfocus()
 */
protected void onUnfocus() {
    _currentPicture = _offPicture;
    invalidate();
}

/**
 * Field implementation.  
 * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
 */
protected void drawFocus(Graphics graphics, boolean on) {
    // Do nothing
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#layout(int, int)
 */
protected void layout(int width, int height) {
    setExtent(Math.min( width, getPreferredWidth()), 
    Math.min( height, getPreferredHeight()));
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#paint(Graphics)
 */
protected void paint(Graphics graphics){       
    // First draw the background colour and picture
    graphics.setColor(this.color);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);

    // Then draw the text
    graphics.setColor(Color.BLACK);
    graphics.setFont(_font);
    graphics.drawText(_label, 4, 2, 
        (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
        getWidth() - 6 );
}

/**
 * Overridden so that the Event Dispatch thread can catch this event
 * instead of having it be caught here..
 * @see net.rim.device.api.ui.Field#navigationClick(int, int)
 */
protected boolean navigationClick(int status, int time){
    fieldChangeNotify(1);
    return true;
}

}

您要推送的新屏幕类的名称是什么?例如,主屏幕?谢谢您的代码UserInterface.getUiApplication().pushScreen(new HomeScreen());真管用!但是,我无法在super()下运行按钮函数。在模拟器中它总是会给我一个错误。我几乎复制粘贴了您在我的super()调用下面提供的示例,并在另一个文件中创建了一个MyHomeScreen类。当我注释掉按钮代码时,没有错误。我最终能够通过位图click navigationClick()调用该函数。你知道为什么我不能在没有模拟器错误的情况下调用代码的按钮部分吗?104。谢谢你,Richard,你提供的命令工作得很好。我肯定也会看你的博客页面。你知道rfsk2010在下面的帖子中(在评论部分解释)的问题是什么吗?我似乎无法调用代码的按钮部分。没有跳出任何内容。你从模拟器中得到了什么错误?应用程序错误104未捕获的NullPointerException好问题,你可能必须尝试插入一些try{}catch{}块,并找出NullPointer正在使用的位置。谢谢,我会尝试一下。它会是类似于try{这里测试的代码}catch{exception e}的东西吗?
package com.pl.button;

import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;

public class imagefield extends Field {

private String _label;
private int _labelHeight;
private int _labelWidth;
private Font _font;

private Bitmap _currentPicture;
private Bitmap _onPicture; 
private Bitmap _offPicture; 
int color;

public imagefield (String text, long style ,String img, String img_hvr, int color){
    super(style);


    _offPicture = Bitmap.getBitmapResource(img);
    _onPicture = Bitmap.getBitmapResource(img_hvr);

    _font = getFont();
    _label = text;
    _labelHeight = _onPicture.getHeight();  
    _labelWidth = _onPicture.getWidth();

    this.color = color;

    _currentPicture = _offPicture;
}

/**
 * @return The text on the button
 */
String getText(){
    return _label;
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#getPreferredHeight()
 */
public int getPreferredHeight(){
    return _labelHeight;
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#getPreferredWidth()
 */
public int getPreferredWidth(){
    return _labelWidth;
}

/**
 * Field implementation.  Changes the picture when focus is gained.
 * @see net.rim.device.api.ui.Field#onFocus(int)
 */
protected void onFocus(int direction) {
    _currentPicture = _onPicture;
    invalidate();
}

/**
 * Field implementation.  Changes picture back when focus is lost.
 * @see net.rim.device.api.ui.Field#onUnfocus()
 */
protected void onUnfocus() {
    _currentPicture = _offPicture;
    invalidate();
}

/**
 * Field implementation.  
 * @see net.rim.device.api.ui.Field#drawFocus(Graphics, boolean)
 */
protected void drawFocus(Graphics graphics, boolean on) {
    // Do nothing
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#layout(int, int)
 */
protected void layout(int width, int height) {
    setExtent(Math.min( width, getPreferredWidth()), 
    Math.min( height, getPreferredHeight()));
}

/**
 * Field implementation.
 * @see net.rim.device.api.ui.Field#paint(Graphics)
 */
protected void paint(Graphics graphics){       
    // First draw the background colour and picture
    graphics.setColor(this.color);
    graphics.fillRect(0, 0, getWidth(), getHeight());
    graphics.drawBitmap(0, 0, getWidth(), getHeight(), _currentPicture, 0, 0);

    // Then draw the text
    graphics.setColor(Color.BLACK);
    graphics.setFont(_font);
    graphics.drawText(_label, 4, 2, 
        (int)( getStyle() & DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK ),
        getWidth() - 6 );
}

/**
 * Overridden so that the Event Dispatch thread can catch this event
 * instead of having it be caught here..
 * @see net.rim.device.api.ui.Field#navigationClick(int, int)
 */
protected boolean navigationClick(int status, int time){
    fieldChangeNotify(1);
    return true;
}