s:CheckBox - 获取选定的值

时间:2011-05-04 13:57:10

标签: actionscript-3 flash-builder

    <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="Settings" 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>

当我按下按钮时,我收到错误:

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

我做错了什么?

如果我先切换到第二个标签页然后返回并按下按钮,它就会起作用。

2 个答案:

答案 0 :(得分:0)

您可能处于错误的上下文中,并且没有cbAlwaysOnTop的引用。

您的按钮在哪里?您在哪里有处理代码?

编辑:啊,你的评论在底部,我想我知道发生了什么,似乎NavigatorContent只是在你导航时创建它的内容,所以如果你试图在没有先打开标签的情况下访问CheckBox,它尚未创建并抛出空引用错误。

由于我猜你希望设置保持不变,解决方案是创建一个PresentationModel类并绑定CheckBox值,这样你就可以在会话之间保存它并在不依赖UI元素的情况下进行检索。阅读这篇精彩的文章,了解您需要做什么:http://riarockstars.com/2011/03/16/presentation-model-and-multiple-screens-part-1/

答案 1 :(得分:0)

好的,我找到了一个简单的解决方案:

creationPolicy="all" 

在:

中使用它
<mx:TabNavigator  id="x1" x="0" y="0" width="100%" height="100%" creationPolicy="all">

可以解决问题。

相关问题