返回值而不是null

返回值而不是null

问题描述:

我想调用一个函数并传递一些属性给它,但它是抱怨我试图将目标对象为null。任何人都可以看到我要去哪里吗?返回值而不是null

<mx:ViewStack id="vs" width="100%" height="100%" y="53"> 
    <mx:Canvas id="view1" label="Community" width="100%" height="100%" backgroundColor="#ff9900" showEffect="WipeDown" hideEffect="WipeUp"> 
     <mx:Label text="Community"/> 
    </mx:Canvas> 
    <mx:Canvas id="view2" label="Apps" width="100%" height="100%" backgroundColor="green"> 
     <mx:HTML id="myHTML" width="100%" height="100%" 
     visible="true" 
     paintsDefaultBackground="true" 
     htmlRender="browser_completeHandler(event)" 
     locationChange="browser_locationChangeHandler(event)" 
     complete="browser_completeHandler(event)" /> 
    </mx:Canvas> 
</mx:ViewStack> 


<local:DockBar id="dockbar" horizontalCenter="0" bottom="0" width="100%" height="100" minSize="32" maxSize="80"> 
    <mx:Label visible="false" id="menuLabel" text="Menu" bottom="0" horizontalCenter="0" fontSize="24" color="#ffffff" alpha=".75" /> 
    <mx:Image click="gotoApp('Google','http://www.google.com/')" source="{icon1}" buttonMode="true" useHandCursor="true" toolTip="Nice red" rollOver="turnOn(event)" rollOut="turnOff(event)" /> 
    <mx:Image click="gotoApp('Yahoo','http://www.yahoo.com/')" source="{icon2}" buttonMode="true" useHandCursor="true" toolTip="Cool orange" rollOver="turnOn(event)" rollOut="turnOff(event)" /> 
    </mx:HBox> 
</local:DockBar> 

和功能如下:

private function gotoApp(id:String,url:String):void { 
    vs.selectedChild=view4; 
    trace(myHTML); 
} 

它返回null我第一次单击图像然而随后的尝试跟踪的值(我假设,因为它设置,那么,就不能当应用程序加载)。任何想法如何在应用程序加载时识别它?

干杯

井“view4”是不是你viewstack的孩子,所以它会当它试图对它的孩子转移到抛出一个空指针。

的原因,它不把它第二次可能是由于像

public function set selectedChild(child : Object) : void { 
    if (child == _selectedChild) return; 
    ... 
} 

这是在setter方法很常见的模式。

假设“view4”是一个错字,你的意思是“view2”,你可以将ViewStack中的creationPolicy设置为“all”。默认情况下,ViewStacks只在创建时初始化第一个视图,将creationPolicy设置为“all”会强制堆栈中的所有视图得到实例化。

默认情况下,ViewStack将以文档顺序懒惰地创建其子级,因此只有在加载时可见的第一个子元素(即view1)在您的函数中不会有空id。随后点击ViewStack将创建其他视图(view2,view3和view4),这就是错误不再发生的原因。使用selectedIndex属性,而不是

<mx:ViewStack id="vs" creationPolicy="all" width="100%" height="100%" y="53"> 
    <mx:Canvas id="view1" label="Community" width="100%" height="100%"> 
     ... 
    </mx:Canvas> 
     ... 
    <mx:Canvas id="view4" label="Apps" width="100%" height="100%"> 
     ... 
    </mx:Canvas> 
</mx:ViewStack> 

尝试设置:

所以,你需要修改代码以包括的creationPolicy = “all” 来解决这个问题

vs.selectedIndex = 1; 

这样你”不要失去推迟实例化的好处。