簡體   English   中英

Flex TabNavigator null對象問題

[英]Flex TabNavigator null object issue

我在使用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時,它將跳轉到設置頁面。 這將顯示tabnavigator。 再次按F1可隱藏它。

基本上,重復顯示和隱藏選項卡導航器沒有問題。 但是,如果我在隱藏選項卡導航器之前單擊任何選項卡,當我嘗試通過按鍵盤上的F1再次顯示選項卡導航器時,它會發送錯誤,如上所示。

如何解決/防止錯誤發生?

謝謝。

看起來你偶然發現了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進入settingTabNavigator最初不在displayList上(你正在通過includeIn添加它),因此它沒有focusManager 那是完全正常的; 不正常的是,對於當前不在displayList上的組件執行keyDownHandler方法。 這最終會導致nullpointer異常,因為執行該方法時沒有focusManager
看起來框架在刪除TabNavigator的父級時不會清除事件偵聽器。 當使用includeIn本身添加/刪除TabNavigator時,它確實可以正常工作,所以幸運的是,解決方法非常簡單:

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

您可能希望在Apache Flex JIRA中注冊此錯誤

我剛剛找到了防止bug發生的方法。 那是通過添加TabNavigator.stage.focus=null; 在將狀態更改回State1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM