s:CheckBox - getting selected value
<fx:Script>
<![CDATA[
protected function button1_clickHandler(event:MouseEvent):void
{
if (cbAlwaysOnTop.selected) { // <<<<<< I get the error #1009 here
} else {
}
}
]]>
</fx:Script>
<mx:TabNavigator x="0" y="0" width="100%" height="100%">
<s:NavigatorContent label="Translate" width="100%" height="100%">
<s:Button label="test" click="button1_clickHandler(event)"/>
</s:NavigatorContent>
<s:NavigatorContent label="Settin开发者_StackOverflowgs" width="100%" height="100%">
<s:CheckBox x="10" y="22" label="always on top" selected="true" click="checkbox1_clickHandler(event)" id="cbAlwaysOnTop"/>
</s:NavigatorContent>
</mx:TabNavigator>
When I press the button I get the error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
What am I doing wrong?
It works if I first switch to the 2nd tabpage and then back and press the button.
You are probably in the wrong context and not have a reference to cbAlwaysOnTop
.
Where is your button and where do you have the handling code?
Edit: ah, with your comment on the bottom I think I know what's going on, seems that NavigatorContent is only creating its contents when you navigate there, so if you try to access the CheckBox inside without first opening the tab, it hasn't been created and throwing you a null reference error.
Since I guess you want the settings to persist, the solution would be to create a PresentationModel class and bind the CheckBox value, so you can both save it between sessions and retrieve without relying on a UI element. Read this great article to understand what you need to do: http://riarockstars.com/2011/03/16/presentation-model-and-multiple-screens-part-1/
Ok, I found an easy solution to this using:
creationPolicy="all"
Using this in:
<mx:TabNavigator id="x1" x="0" y="0" width="100%" height="100%" creationPolicy="all">
would solve the problem.
精彩评论