开发者

Error when passing variable from one Component to another in Flex

开发者 https://www.devze.com 2023-04-05 14:57 出处:网络
I am getting error when trying to pass a variable from one to component to another. I have a main MXML and 2 components. My files are as follows:

I am getting error when trying to pass a variable from one to component to another. I have a main MXML and 2 components. My files are as follows:

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/200开发者_开发问答9" 
         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.

Can anyone help on this?


Give your components Id's

Then you can just go MyComp.SomePublicVar = value

From the second comp you could go Alert.show(parentDocument.parentDocument.MyComp.SomePublicVar);

Something like that ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消