在Flex中将变量从一个Component传递到另一个Component时出错

时间:2011-09-19 08:02:04

标签: flex4 components

尝试将变量从一个组件传递到另一个组件时,我遇到错误。     我有一个主要的MXML和2个组件。我的文件如下:

Main MXML

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               height="100%" width="100%" xmlns:comps="components.*">


    <s:layout >
        <s:VerticalLayout/>
    </s:layout>


    <s:Group width="100%" height="100%">

            <s:HGroup>
                <habilitations:CMS comps="{this}" width="637" height="48"/>
            </s:HGroup>

            <s:Spacer/>

            <s:HGroup height="70%" width="30%">
                <comps:Component1 comps="{this}"/>
<comps:Component2 comps="{this}"/>
            </s:HGroup>
</s:Application>




Component1


<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
         xmlns:s="library://ns.adobe.com/flex/spark"
         xmlns:mx="library://ns.adobe.com/flex/mx"
         width="400" height="100%">
    <s:layout>
        <s:VerticalLayout/>
    </s:layout>

    <fx:Script>
        <![CDATA[
            import mx.collections.XMLListCollection;
            import mx.controls.Alert;
            import mx.events.FlexEvent;

            [Bindable]
            public var testvar:String ="MYTEST";

            [Bindable]
            public var mainMXML:MainMXML;

        ]]>
    </fx:Script>


</s:Group>




Component2

<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;


            [Bindable]
            public var mycomponent1:Component1;
            [Bindable]
            public var mainMXML:MainMXML;

            protected function clickHandler(event:MouseEvent):void
            {
                Alert.show(new String( mycomponent1.testvar ));

            }


]]>
    </fx:Script>


    <s:Button click="clickHandler(event)"/>

</s:Group>



But I am getting the error :

Error #1009: Cannot access a property or method of a null object reference.

有人可以为此提供帮助吗?

1 个答案:

答案 0 :(得分:0)

提供您的组件ID

然后你可以去MyComp.SomePublicVar = value

从第二个补偿你可以去Alert.show(parentDocument.parentDocument.MyComp.SomePublicVar);

类似的东西;)