【问题标题】:Flex TabNavigator null object issueFlex TabNavigator 空对象问题
【发布时间】:2013-12-15 14:59:41
【问题描述】:

我在使用 TabNavigator 时遇到问题,它总是发送错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at mx.containers::TabNavigator/keyDownHandler()[/Users/justinmclean/Documents/ApacheFlex4.11.0/frameworks/projects/mx/src/mx/containers/TabNavigator.as:903]

示例代码:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                   xmlns:s="library://ns.adobe.com/flex/spark" 
                   xmlns:mx="library://ns.adobe.com/flex/mx" applicationComplete="init(event)">
<fx:Script>
    <![CDATA[
        import mx.events.FlexEvent;

        protected function init(event:FlexEvent):void
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
        }

        public function handleKeyDown(event:KeyboardEvent):void
        {
            if(event.keyCode == Keyboard.F1){
                if(currentState=="State1"){
                    currentState = "setting";
                }else{
                    currentState = "State1";
                }
            }
        }
    ]]>
</fx:Script>
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:states>
    <s:State name="State1"/>
    <s:State name="setting"/>
</s:states>

<s:BorderContainer left="0" right="0" top="0" bottom="0" includeIn="setting">
<mx:TabNavigator id="TabNavigator" left="10" right="10" top="110" bottom="10" creationPolicy="all">
    <!-- General Tab -->
    <s:NavigatorContent id="generaltab" width="100%" height="100%" label="General">
        <s:Group id="generalcontainer">
        </s:Group>
    </s:NavigatorContent>
    <!-- Screen Tab -->
    <s:NavigatorContent id="screentab" width="100%" height="100%" label="Screen">
        <s:Group id="screencontainer" width="100%" height="100%">
        </s:Group>
    </s:NavigatorContent>
    <!-- Playlist Tab -->
    <s:NavigatorContent id="playlisttab" width="100%" height="100%" label="Playlist">
        <s:Group id="playlistcontainer">    
        </s:Group>
    </s:NavigatorContent>
</mx:TabNavigator>
    </s:BorderContainer>
</s:WindowedApplication>

当我按下 F1 时,它会跳转到设置页面。这将显示选项卡导航器。再次按 F1 将其隐藏。

基本上重复显示和隐藏标签导航器是没有问题的。但是,如果我在隐藏选项卡导航器之前单击了任何选项卡,当我尝试通过按键盘上的 F1 再次显示选项卡导航器时,它会发出如上所示的错误。

如何解决/防止错误发生?

谢谢。

【问题讨论】:

    标签: flash apache-flex flash-builder tabnavigator


    【解决方案1】:

    您似乎偶然发现了 SDK 中的一个错误(坦率地说,这让我有点担心,因为这意味着一些事件侦听器没有被正确处理)。

    查看错误发生的来源:

    override protected function keyDownHandler(event:KeyboardEvent):void
    {
        if (focusManager.getFocus() == this)
        {
            // Redispatch the event from the TabBar so that it can handle it.
            tabBar.dispatchEvent(event);
        }
    }
    

    发生的情况是,当您从 State1 转到 setting 时,TabNavigator 最初不在 displayList 上(您正在通过 includeIn 添加它),因此它没有 @ 987654327@。这是完全正常的;不正常的是 keyDownHandler 方法是针对当前不在 displayList 上的组件执行的。这最终会导致 nullpointer 异常,因为执行该方法时没有 focusManager
    当 TabNavigator 的父级被移除时,框架似乎没有清理事件侦听器。当使用 includeIn 本身添加/删除 TabNavigator 本身时,它确实可以正常工作,所以幸运的是,解决方法非常简单:

    <s:BorderContainer includeIn="setting">
        <mx:TabNavigator id="TabNavigator" includeIn="setting" creationPolicy="all">
    

    您可能希望在Apache Flex JIRA 中注册此错误

    【讨论】:

    • 嗨 RIAstar。非常感谢您的解释。我会将这个错误提交给 Apache Flex JIRA。但与此同时,您有什么建议可以防止该错误的发生吗?我简直等不及修复的错误继续项目。
    • @user1995781 在 TabNavigator 本身上应用 includeIn="setting" 对我有用。它不适合你吗?
    • 我实际上错过了阅读您的解决方法。无论如何,我只是尝试过,但它似乎对我不起作用。也许它适用于此处的简化示例,但不适用于实际代码。无论如何,非常感谢您的帮助。 :)
    【解决方案2】:

    我刚刚找到了一种防止错误发生的方法。即在将状态更改回State1之前添加TabNavigator.stage.focus=null;

    【讨论】:

      猜你喜欢
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-02-22
      • 2013-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多