2008-11-25 68 views
1

附带的代码示例(伪代码)编译ViewStack selectedChild物业,但抛出此运行时错误:的Flex - 绑定使用字符串值

TypeError: Error #2007: Parameter child must be non-null. 
    at flash.display::DisplayObjectContainer/getChildIndex() 
    at mx.core::Container/getChildIndex()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\core\Container.as:2409] 
    at mx.containers::ViewStack/set selectedChild()[E:\dev\3.0.x\frameworks\projects\framework\src\mx\containers\ViewStack.as:557] 


<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
     <![CDATA[ 
      [Bindable] 
      private var targetViewName:String = "content"; 
     ]]> 
    </mx:Script> 

    <mx:ViewStack id="viewStack" width="100%" height="100%" 
     selectedChild="{Container(viewStack.getChildByName(targetViewName))}"> 
     <mx:Panel id="welcome" width="100%" height="100%" /> 

     <mx:Panel id="content" width="100%" height="100%" /> 
    </mx:ViewStack> 
</mx:Application> 

有一些方法可以让我得到这个无需工作调用一个函数来设置selectedChild?

谢谢。

回答

3

时selectedChild触发则ViewStack没有添加任何孩子所以它抛出一个NullPointerException:

下面的工作:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
     <![CDATA[ 
      import mx.core.Container; 
      [Bindable] 
      private var targetViewName:String = "content"; 

      private function onClick() : void 
      { 
       viewStack.selectedChild = Container(viewStack.getChildByName(targetViewName)) ; 
      } 
     ]]> 
    </mx:Script> 

    <mx:ViewStack id="viewStack" width="100%" height="100%" > 
     <mx:Panel id="welcome" width="100%" height="100%" title="welcome"/> 

     <mx:Panel id="content" width="100%" height="100%" title="content" /> 
    </mx:ViewStack> 

    <mx:Button click="onClick()" label="click" /> 

</mx:Application> 
0

尝试这样做:

selectedChild="{this[targetViewName]}"> 

/尼尔斯

0

对不起,/尼尔斯,这是行不通的。尝试编译这段代码,你会看到selectedChild不会改变(你也得到一个编译警告):

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> 
    <mx:Script> 
     <![CDATA[ 
      [Bindable] 
      private var targetViewName:String = "content"; 
     ]]> 
    </mx:Script> 

    <mx:TabNavigator id="viewStack" width="100%" height="100%" creationPolicy="all" 
     selectedChild="{this[targetViewName]}"> 
     <mx:Panel id="welcome" width="100%" height="100%" label="welcome" /> 

     <mx:Panel id="content" width="100%" height="100%" label="content" /> 
    </mx:TabNavigator> 
</mx:Application> 
0

我的猜测是,这是行不通的,因为绑定将在初始化时在此进行评估时,时间,Viewstack的孩子还没有创建。即使将creationPolicy设置为“全部”,问题仍然存在。

当创建视图堆栈(也可能是子视图)时,您将不得不建立一个到targetViewName的绑定。

0

想要在目标位于显示列表中时设置selectedChild属性。试试这个:

<mx:TabNavigator id="viewStack" width="100%" height="100%" creationPolicy="all" > 
    <mx:Panel id="welcome" width="100%" height="100%" label="welcome" /> 

    <mx:Panel id="content" width="100%" height="100%" label="content" addedToStage="viewStack.selectedChild = this" /> 
</mx:TabNavigator> 

如果你真的要绑定selectedChild,然后创建一个返回您想要选择面板中的绑定功能,但只有当它的viewStack的孩子。

0
<mx:Script> 
    <![CDATA[ 
     import models.ModelLocator; 

     [Bindable] 
     private var model:ModelLocator = ModelLocator.getInstance(); 
    ]]> 
</mx:Script> 

<mx:ViewStack id="videoViewStack" width="100%" height="100%" selectedChild="{this[model._videoViewStack]}" > 
    <viewsVideos:AllVideos id="AllVideos" label="Videos"/> 
    <viewsVideos:MainVideo id="MainVideo" label="Video"/> 
</mx:ViewStack> 

这个结合的字符串变种我得到一个警告,但它的作品