1

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

图形用户界面

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

滚动左侧内容并删除左侧 VFM 后出现问题,然后仅显示 Home 按钮,并且触控板导航(滚动)导致 Null Pointer 异常屏幕。下图包含错误的模拟器屏幕截图。

错误画面

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

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);    
                }
            }
        }
    };
}

我该如何解决这个问题?

提前致谢

问候

阿尼什

4

1 回答 1

0

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

摆脱这个:

synchronized (UiApplication.getEventLock()) {

用这个替换它:

UiApplication.getInstance().invokeLater(new Runnable{ public void run() {
于 2013-02-11T20:44:00.857 回答