为什么我的覆盖保护函数createChildren被忽略?

时间:2009-06-02 00:24:25

标签: flex actionscript-3

以下是错误:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
        at view::ScoreBoard/setChipCount()[C:\Flex Builder 3\StackOverflowQuestion\src\view\ScoreBoard.as:32]
        at model::MainDeckScoreBoard()[C:\Flex Builder 3\StackOverflowQuestion\src\model\MainDeckScoreBoard.as:21]
        at model::MainDeckScoreBoard$cinit()
        at global$init()[C:\Flex Builder 3\StackOverflowQuestion\src\model\MainDeckScoreBoard.as:5]
        at main()[C:\Flex Builder 3\StackOverflowQuestion\src\main.mxml:13]
        at _main_mx_managers_SystemManager/create()
        at mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3188]
        at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::docFrameHandler()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3064]
        at mx.managers::SystemManager/docFrameListener()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:2916]

这是main.mxml:

    <?xml version="1.0"?>
    <mx:Application
        xmlns:mx="http://www.adobe.com/2006/mxml"
        creationComplete="initApp()"
    >
        <mx:Script>
            <![CDATA[
                import model.MainDeckScoreBoard;

                public var _mainDeckScoreBoard:MainDeckScoreBoard = MainDeckScoreBoard.instance;

                private function initApp():void {
                    this.addChild(_mainDeckScoreBoard);
                }
            ]]>
        </mx:Script>
    </mx:Application>

这是MainDeckScoreBoard.as:

package model {
    import view.ScoreBoard;

    [Bindable]
    public dynamic class MainDeckScoreBoard extends ScoreBoard {

        /** Storage for the singleton instance. */
        private static const _instance:MainDeckScoreBoard = new MainDeckScoreBoard( SingletonLock );

        /** Provides singleton access to the instance. */
        public static function get instance():MainDeckScoreBoard {
            return _instance;
        }

        public function MainDeckScoreBoard( lock:Class ) {
            super();
            // Verify that the lock is the correct class reference.
            if ( lock != SingletonLock ) {
                throw new Error( "Invalid Singleton access.  Use MainDeckScoreBoard.instance." );
            }
            this.setChipCount("0");
        }

    } // end class
} // end package

class SingletonLock {
} // end class

这是ScoreBoard.as:

package view {
    import mx.containers.HBox;
    import view.ScoreBoardLabel;
    import view.ChipCountContainer;
    import view.CardRankList;

    public dynamic class ScoreBoard extends HBox {

        /** The chip count. */
        public var _chipCount:ChipCountContainer;

        public function ScoreBoard() {

            super();

            this.width = 489;
            this.height = 40;
            this.setStyle("horizontalScrollPolicy", "off");
        }

        override protected function createChildren():void {

            super.createChildren();

            if(!_chipCount) {
                _chipCount = new ChipCountContainer();
                this.addChild(_chipCount);
            }
        }

        public function setChipCount(labelText:String):void {
            _chipCount._chipCountLabel.text = labelText;
            invalidateDisplayList();
        }
    }
}

这是ChipCountContainer.as:

package view {
    import mx.containers.Canvas;

    public dynamic class ChipCountContainer extends Canvas {

        /** The label. */
        public var _chipCountLabel:ChipCountLabel;

        public function ChipCountContainer() {

            super();

            this.width = 20;
            this.height = 20;
        }

        override protected function createChildren():void {

            super.createChildren();

            if(!_chipCountLabel) {
                _chipCountLabel = new ChipCountLabel();
                this.addChild(_chipCountLabel);
            }
        }
    }
}

我有条不紊地移动了一些东西,并在执行创造儿童舞蹈时挥舞着无效的显示列表香,但我只是成功地完全混淆了自己。我已经在Flex库中搜索了类似的结构,它对我来说看起来还不错,但我想我只是没有得到这个概念。

1 个答案:

答案 0 :(得分:2)

我认为你混淆了实例化的顺序。也就是说,如果要在组件的子组件初始化之后使用setChipCount,则应等待初始化事件触发,即:

    public dynamic class MainDeckScoreBoard extends ScoreBoard {

    ...

        public function MainDeckScoreBoard( lock:Class ) {
            super();
            // Verify that the lock is the correct class reference.
            if ( lock != SingletonLock ) {
                throw new Error( "Invalid Singleton access.  Use MainDeckScoreBoard.instance." );
            }

            // wait for the children to be created
            addEventListener(FlexEvent.INITIALIZE, onInitialize);
        }

        // executes when the children of this component have been created
        private function onInitialize(event:FlexEvent):void {
            this.setChipCount("0");
        }

    } // end class

有关组件实例化生命周期的更详细说明,此adobe doc可能会有所帮助:

http://livedocs.adobe.com/flex/3/html/help.html?content=components_06.html