Blackberry Field Manager Scrolling显示NullpointerException

时间:2013-02-11 10:58:41

标签: blackberry blackberry-simulator blackberry-jde

我遇到Blackberry横向和纵向现场经理的问题。在我的示例应用程序中,我将两个垂直字段管理器添加到一个水平字段管理器中,GUI如下图所示。

GUI Screen

在按钮的字段更改侦听器中,我将删除左侧VFM,然后仅显示“主页”按钮,在主页按钮侦听器中,我将添加左侧VFM,然后返回到上面的屏幕。

滚动左侧内容并删除左侧VFM后出现问题,然后仅显示“主页”按钮,并且跟踪板导航(滚动)将导致“空指针异常”屏幕。下图包含错误的模拟器屏幕截图。

ERROR SCREEN

以下是重现错误的完整源代码。

public class MyApp extends UiApplication{
    public static void main(String[] args)
    {
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        MyApp theApp = new MyApp();       
        theApp.enterEventDispatcher();
    }

    /**
     * Creates a new MyApp object
     */
    public MyApp()
    {        
        // Push a screen onto the UI stack for rendering.
        pushScreen(new MyScreen());
    }
}

public final class MyScreen extends MainScreen{

    private HorizontalFieldManager hfmMainManager = null;

    private VerticalFieldManager menuButtonManager = null;

    private String[] buttons = new String[]{"BUTTON ID 1",
            "BUTTON ID 2",
            "BUTTON ID 3",
            "BUTTON ID 4",
            "BUTTON ID 5",
            "BUTTON ID 6",
            "BUTTON ID 7",
            "BUTTON ID 8",
            "BUTTON ID 9",
            "BUTTON ID 10"};

    private boolean menuShowing = false;

    /**
     * Creates a new MyScreen object
     */
    public MyScreen()
    {        
        super( Manager.NO_VERTICAL_SCROLL | MainScreen.NO_VERTICAL_SCROLLBAR
                | Manager.NO_HORIZONTAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR);

        hfmMainManager = new HorizontalFieldManager(Manager.NO_HORIZONTAL_SCROLL
                | Manager.NO_HORIZONTAL_SCROLLBAR);

        ////////////////////MENU BUTTONS
        menuButtonManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL | Manager.NO_VERTICAL_SCROLLBAR);

        menuButtonManager.add(new ButtonField("Header Button", Field.FIELD_HCENTER));
        VerticalFieldManager vfmScrollable = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR);
        for (int i = 0; i < buttons.length; i++) {

            ButtonField buttonField = new ButtonField(buttons[i], Field.FIELD_HCENTER);
            buttonField.setChangeListener(listenerButton);
            vfmScrollable.add(buttonField);
        }
        menuButtonManager.add(vfmScrollable);

        hfmMainManager.add(menuButtonManager);


        VerticalFieldManager vfmMenuContentManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLLBAR | Manager.NO_VERTICAL_SCROLL);


        ButtonField buttonField = new ButtonField("Home Button", Field.FIELD_HCENTER);
        buttonField.setChangeListener(listenerButton);
        buttonField.setMargin(10, 0, 10, 0);
        vfmMenuContentManager.add(buttonField);

        hfmMainManager.add(vfmMenuContentManager);

        add(hfmMainManager);

        menuShowing = true;
    }


    private FieldChangeListener listenerButton = new FieldChangeListener() {

        public void fieldChanged(Field arg0, int arg1) {

            synchronized (UiApplication.getEventLock()) {
                if(menuShowing){
                    menuShowing = false;
                    hfmMainManager.delete(menuButtonManager);
                }else{
                    menuShowing = true;
                    hfmMainManager.insert(menuButtonManager, 0);    
                }
            }
        }
    };
}

如何解决此问题?

提前致谢

此致

阿尼什

1 个答案:

答案 0 :(得分:0)

尝试从焦点或非焦点事件更改字段结构时,我看到了类似的行为。当一个字段在焦点事件中间从屏幕上移除时,焦点跟踪可能会不同步,并导致像这样的异常错误。尝试从抓取UI锁定改为调用invokeLater。这样,更改侦听器可以在删除VFM之前完成执行,保持所有不变量。

摆脱这个:

synchronized (UiApplication.getEventLock()) {

替换为:

UiApplication.getInstance().invokeLater(new Runnable{ public void run() {