2008-11-25 63 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> 

這個結合的字符串變種我得到一個警告,但它的作品