我刚开始使用flex和actionscript。我正在尝试创建一个包含多个状态的小flex应用程序,但是如果我有嵌套的容器,即使我将creationPolicy设置为“all”,也似乎有些对象没有在我预期的情况下被初始化。
我已经将这个问题简化为一个小例子,其中有一个注释块显示了它何时工作。
使用现有代码,我得到了以下错误:"TypeError: Error #1009:无法在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>谢谢你的反馈。
发布于 2010-09-07 10:01:34
由于creationPolicy="all“属性的工作方式不像我想象的那样,所以对于复杂的状态更改,我为每个状态设置了一个‘状态初始化’方法,键控了currentStateChange事件。下面是我所做的一个例子,其中还包括一个使用Flex 4状态的更新,尽管我在示例中没有使用星火组件。
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:s="library://ns.adobe.com/flex/spark"
applicationComplete="init();"
currentStateChange="stateChange(event);"
creationPolicy="all"
currentState="bar"
>
<fx:Script>
import mx.controls.Alert;
import mx.events.StateChangeEvent;
private var InittedStates:Object = new Object();
private function mainButtonHandler(event:Event):void{
currentState = "bar"
}
private function startButtonHandler(event:Event):void {
currentState = "foo";
}
public function stateChange(event:StateChangeEvent):void{
if (event.newState === 'bar'){
initBarState();
}
else if(event.newState === 'foo'){
initFooState();
}
}
public function initBarState():void {
if (InittedStates.bar){
return;
}
startButton.addEventListener(MouseEvent.CLICK, startButtonHandler);
InittedStates.bar = true;
}
public function initFooState():void {
if (InittedStates.foo){
return;
}
mainButton.addEventListener(MouseEvent.CLICK, mainButtonHandler);
InittedStates.foo = true;
}
public function init():void{
}
</fx:Script>
<s:states>
<s:State name="foo"/>
<s:State name="bar"/>
</s:states>
<mx:Panel includeIn="foo" creationPolicy="all">
<mx:VBox creationPolicy="all">
<mx:Button id="mainButton" label="Change to Start State"/>
</mx:VBox>
</mx:Panel>
<mx:Panel includeIn="bar" creationPolicy="all">
<mx:VBox creationPolicy="all">
<mx:Button id="startButton" label="Change to Main state"/>
</mx:VBox>
</mx:Panel>
</s:Application>https://stackoverflow.com/questions/3653230
复制相似问题