简体   繁体   English

动态填充组合框(Flex)

[英]Dynamic populate ComboBox (Flex)

I want to populate a ComboBox after a clicked a button. 我想在单击按钮后填充ComboBox。

This is my code: 这是我的代码:

    var dpNames:ArrayCollection = new ArrayCollection();
    for each(var ca:Categorie in arrCategories)
    {
          dpNames.addItem ({label: ca.name, data: ca.value});
    }       

    cbWijzigCategorie.dataProvider = dpNames;

But when he executes the last line, I alwas get the following error: 但是当他执行最后一行时,我总是得到以下错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference. TypeError:错误#1009:无法访问空对象引用的属性或方法。

I have no idea why. 我不知道为什么。

Thanks a lot, Vincent 非常感谢Vincent

    private function categorieItemClick(evt:ItemClickEvent):void
    {

            var dpNames:ArrayCollection = new ArrayCollection();
            for each( var ca:Categorie in arrCategories)
            {
                dpNames.addItem ({label: ca.Name, data: ca.Name});
            }                   
            cbWijzigCategorie.dataProvider = dpNames;


    }



<mx:ButtonBar 
        x="10" y="36" 
        id="tbtnbarCategorie" 
        dataProvider="vsCategorie" 
        itemClick="categorieItemClick(event)">
</mx:ButtonBar>

    <mx:ViewStack 
        x="10" y="64" 
        id="vsCategorie" 
        width="601" height="343">

           <mx:Canvas label="Wijzig categorie" 
        id="cnvsChangeCategorie" 
        width="100%" height="100%">
        <mx:Label 
        x="10" y="10" 
        text="Kies categorie"/>
             <mx:ComboBox 
        x="101" y="8" 
        id="cbWijzigCategorie"></mx:ComboBox>
           </mx:Canvas>

    </mx:ViewStack>

Is your combobox instanciated? 您的组合框被指示了吗? Looking at the error message, the problem would come from "cbWijzigCategorie.datprovider". 查看错误消息,问题将来自“ cbWijzigCategorie.datprovider”。 It cannot access the dataprovider property cause cbWijzigCategorie is null. 由于cbWijzigCategorie为空,它无法访问dataprovider属性。

Where are you assigning the dataprovider to the combobox? 您在哪里将数据提供者分配给组合框? Inside the or outside the component? 在组件内部还是外部?

Regards. 问候。

确保arrCategories不为空并且已正确初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM