【发布时间】:2010-09-06 17:25:37
【问题描述】:
我是 flex 和 actionscript 的新手。我正在尝试创建一个具有多个状态的小型 flex 应用程序,但是如果我有嵌套容器,看起来有些对象在我期望的时候没有被初始化,即使我将 creationPolicy 设置为“all 。”
我已将问题简化为一个小示例,并在其确实工作时显示了一个注释块。
使用现有代码,我收到此错误:“TypeError: Error #1009: Cannot access a property or method of an null object reference at main/init()”并且未安装事件处理程序。
如果我改为使用已删除 Panel 和 VBox 元素的注释块,它确实工作。
我知道我可以为 mxml 元素添加一个 click 属性,但这只是一个简化的示例,我更想知道为什么在应用加载时对象没有被初始化。
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
applicationComplete="init();"
currentState="start">
<mx:Script>
private function mainButtonHandler(event:Event):void{
currentState = "start"
}
private function startButtonHandler(event:Event):void {
currentState = "main";
}
public function init():void{
mainButton.addEventListener(MouseEvent.CLICK, mainButtonHandler);
startButton.addEventListener(MouseEvent.CLICK, startButtonHandler);
}
</mx:Script>
<!-- this does not work -->
<mx:states>
<mx:State name="main">
<mx:AddChild creationPolicy="all">
<mx:Panel creationPolicy="all">
<mx:VBox creationPolicy="all">
<mx:Button id="mainButton" label="Change to Start State"/>
</mx:VBox>
</mx:Panel>
</mx:AddChild>
</mx:State>
<mx:State name="start">
<mx:AddChild creationPolicy="all">
<mx:Panel creationPolicy="all">
<mx:VBox creationPolicy="all">
<mx:Button id="startButton" label="Change to Main state"/>
</mx:VBox>
</mx:Panel>
</mx:AddChild>
</mx:State>
</mx:states>
<!-- this works -->
<!--
<mx:states>
<mx:State name="main">
<mx:AddChild creationPolicy="all">
<mx:Button id="mainButton" label="Change to Start State"/>
</mx:AddChild>
</mx:State>
<mx:State name="start">
<mx:AddChild creationPolicy="all">
<mx:Button id="startButton" label="Change to Main state"/>
</mx:AddChild>
</mx:State>
</mx:states>
-->
</mx:Application>
感谢您的任何反馈。
【问题讨论】: