为啥我的覆盖保护函数 createChildren 被忽略?
Posted
技术标签:
【中文标题】为啥我的覆盖保护函数 createChildren 被忽略?【英文标题】:Why is my override protected function createChildren being ignored?为什么我的覆盖保护函数 createChildren 被忽略? 【发布时间】:2010-10-30 13:27:30 【问题描述】:这是错误:
TypeError:错误 #1009:无法访问空对象引用的属性或方法。 在视图::ScoreBoard/setChipCount()[C:\Flex Builder 3\***Question\src\view\ScoreBoard.as:32] 在模型::MainDeckScoreBoard()[C:\Flex Builder 3\***Question\src\model\MainDeckScoreBoard.as:21] 在模型::MainDeckScoreBoard$cinit() 在 global$init()[C:\Flex Builder 3\***Question\src\model\MainDeckScoreBoard.as:5] 在 main()[C:\Flex Builder 3\***Question\src\main.mxml:13] 在 _main_mx_managers_SystemManager/create() 在 mx.managers::SystemManager/initializeTopLevelWindow()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\managers\SystemManager.as:3188] 在 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] 在 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】:我认为您混淆了实例化的顺序。即,如果要在组件的子组件初始化后使用 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 文档可能会有所帮助:
http://livedocs.adobe.com/flex/3/html/help.html?content=components_06.html
【讨论】:
以上是关于为啥我的覆盖保护函数 createChildren 被忽略?的主要内容,如果未能解决你的问题,请参考以下文章