2011-05-04 176 views
0
<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> 

當我按下按鈕,我得到的錯誤:S:複選框 - 選擇得到價值

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

我在做什麼錯?

它工作,如果我第一次切換到第二個頁面,然後回來,然後按下按鈕。

回答

0

好吧,我發現了一個簡單的解決這個使用:

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

可以解決這個問題:

creationPolicy="all" 

在使用本。

0

您可能是在錯誤的背景下,並沒有參考cbAlwaysOnTop

你的按鈕在哪裏,你有哪些處理代碼?

編輯:啊,在你對底部的評論中,我想我知道發生了什麼,看起來NavigatorContent只是在你瀏覽時創建它的內容,所以如果你試圖在沒有首先打開標籤的情況下訪問CheckBox,它還沒有被創建並且拋出一個空引用錯誤。

因爲我想你想讓設置保持不變,所以解決方案是創建一個PresentationModel類並綁定CheckBox值,這樣既可以在會話之間保存它,也可以在不依賴UI元素的情況下進行檢索。閱讀這篇大文章,瞭解你需要做什麼:http://riarockstars.com/2011/03/16/presentation-model-and-multiple-screens-part-1/

+0

我增加了更多的例子代碼。 – Guus 2011-05-04 14:16:59

+0

好的,謝謝我會閱讀文章。 – Guus 2011-05-04 14:43:52