0
尝试将变量从一个组件传递到另一个组件时出现错误。 我有一个主要的MXML和2个组件。我的档案如下:在Flex中将变量从一个组件传递到另一个组件时出现错误
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.
任何人都可以帮忙吗?
所以,如果你给的Component1一个正确的ID =“”属性代码将被。 Alert.show(new String(parentDocument.parentDocument.mycomponent1.testvar)); –
我只需要像这样添加它: Alert.show(new String(parentDocument.mycomponent1.testvar)); 为什么添加parentDocument 2次? – FlexyBoz
取决于嵌套的深度,您可以随意多跳几次,但无法跳过最顶层的父级,所以我只是将其作为习惯加倍,以防组件嵌套在另一个组件中。 但你是对的在你的例子中的跳跃级别是好的。 –