我是新來flex和actionscript。我正在嘗試創建一個具有多個狀態的小型Flex應用程序,但是如果我有嵌套容器,那麼當我預期它們是某些對象時,它們看起來不會被初始化,即使我將creationPolicy設置爲「all 「。爲什麼我得到這個「無法訪問空對象引用的屬性或方法」錯誤?
我已將這個問題簡化爲一個小例子,其中有一個註釋塊顯示它何時確實有效。
使用現有的代碼,我得到此錯誤:「TypeError:錯誤#1009:不能訪問主/ 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>
感謝您的任何反饋。
Eugene,我想我明白了,但是我可以期望(或強制)給定上下文中的所有mxml生成對象,例如狀態,以便初始化嗎?何時可以安全地使用動作腳本來觸摸? – mshiltonj 2010-09-08 11:23:16
一個簡短的壞方法,只是添加創建完整的事件處理程序給每個組件,在其他情況下,我應該研究一下,我會讓你知道的 – Eugene 2010-09-08 14:47:50