2012-03-11 52 views
0

我正在嘗試開發基於移動的Flex應用程序。Flex移動ArrayCollection錯誤

在我的申請中,我有兩個意見。

我試圖從一個視圖傳遞的ArrayCollection作爲數據另一種觀點,而是試圖在第二視圖訪問的ArrayCollection,我得到一個錯誤..

這裏的代碼firstView.mxml

dirSteps的是,我試圖傳遞給下一個視圖中的ArrayCollection ...

​​

代碼從DetailDirection.mxml

[Bindable] 
private var directionList:ArrayCollection; 

private function init():void { 
    directionList = new ArrayCollection(ArrayUtil.toArray(data)); 

    // here, data should be my arraycollection, but throws above error on 
    // trying to access property (i.e Step, Distance etc ..) of ArrayCollection ... 
    trace(data.Distance); 
} 

Error: Unknown Property: 'Distance'. at mx.collections::ListCollectionView/http://www.adobe.com/2006/actionscript/flash/proxy::getProperty()[E:\dev\4.y\frameworks\projects\framework\src\mx\collections\ListCollectionView.as:870] at views::DetailDirection/init()[C:\Documents and Settings\ARSENAL\Adobe Flash Builder 4.6\CityExplorer_v2.0\src\views\DetailDirection.mxml:21] at views::DetailDirection/___DetailDirection_View1_creationComplete()[C:\Documents and Settings\ARSENAL\Adobe Flash Builder 4.6\CityExplorer_v2.0\src\views\DetailDirection.mxml:6] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:13152] at mx.core::UIComponent/set initialized()[E:\dev\4.y\frameworks\projects\framework\src\mx\core\UIComponent.as:1818] at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:842] at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.y\frameworks\projects\framework\src\mx\managers\LayoutManager.as:1180]


是什麼原因造成這個錯誤?需要做什麼 ?

我在這裏失蹤的任何東西?

感謝

回答

0

距離不上一個ArrayCollection一個文件的屬性,就是爲什麼它拋出一個錯誤。

data屬性通常是泛型對象;當直接訪問屬性時,Flex編譯器通常不會拋出編譯錯誤。

您可能想要訪問ArrayCollection中的項目;像這樣:

((data as ArrayCollection).getItemAt(0) as MyObjectType).distance 
+0

什麼是'MyObjectType'這裏 ?你在談論哪種類型的對象? – tomjerry 2012-03-11 04:44:42

+0

^謝謝。你的soln工作。但是,當我嘗試在第二個視圖中存儲'data'的值時,像這樣.. 'private var arr:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(data));'然後嘗試訪問as .. '((arr as ArrayCollection).getItemAt(0)as MyObjectType).distance' ..它仍然拋出與「距離屬性未知」相同的錯誤... – tomjerry 2012-03-11 04:53:20

+0

@tomjerry「MyObjectType」將是對象類型dataProvider中的對象。當你像這樣初始化ArrayCollection時,它被設置一次;可能在數據屬性設置之前。當您嘗試訪問值以查看錯誤時,您必須逐步完成代碼。 ArrayCollection是否有效?您嘗試訪問的項目是否存在於ArrayCollection中?它是否正確投射?最後,它是否有距離屬性? – JeffryHouser 2012-03-11 13:03:13

0

這裏data是一個ArrayCollection。所以,你不能直接訪問data.Distance

data[index]會給你的對象,所以data[index].Distance

如:

var data:ArrayCollection = new ArrayCollection(); 

data.addItem({name:"jack", distance:300}); 
data.addItem({name:"jill", distance:400}); 

trace(data[1].distance); // prints 400 

在你的情況使用循環

private function init():void { 

    for(var i:int = 0; i < data.length; i++){ 

     var item:Object = data[i]; 
     trace(item.Distance); 

    } 

} 
+0

只有在同一視圖中嘗試訪問arraycollection時,您的soln纔有效。但是我想將arraycollection傳遞給secondview並訪問它。所以,在這種情況下,你的soln可能不起作用。 – tomjerry 2012-03-11 04:42:19

+0

我只是展示瞭如何訪問添加到ArrayCollection的對象。你必須給索引。 – Diode 2012-03-11 04:45:37

+0

是的,但如果我試圖從第二個視圖訪問它,如何訪問? – tomjerry 2012-03-11 04:47:03