Flex 错误 #1009:无法访问空对象引用的属性或方法

问题描述:

我正在尝试在我的 init() 方法中使用一个按钮.

I am trying to use a button in my init() method.

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="Test"
        creationComplete="init()">

现在,当我尝试用我的按钮做某事时,我收到了提到的错误.我假设它可能还没有加载?

Now when I try to do something with my button I get the error mentioned. I am assuming maybe it has not loaded yet?

function init():void{
     myButton.thisorthat == makes the error.
}

*编辑**该按钮是在 MXML 中创建的 btw这并不重要,但这是针对 flex 移动应用的.

*EDIT** The button is created in MXML btw Not that it matters, but this is for a flex mobile app.

实际上确实很重要.关于 NavigatorContent(假设您的孩子是这些类型容器之一的一个子集)要记住的一件事是他们都将内容创建策略设置为延迟 - 这意味着它创建了视图/视图堆栈的最父层,但在用户实际导航到该特定孩子之前,它不是孩子.一种欺骗是将策略设置为ALL",但更好的方法是实际监听 FlexEvent.CONTENT_CREATION_COMPLETE(这是从导航的子级广播容器).

Actually it does matter. One thing about NavigatorContent (assuming your children are a subset of one of these types of containers) to remember along with their halo counterparts is they all have a content creation policy set to deferred - meaning it creates the parent most layer of the view / viewstack, but not it's children until the user has actually navigated to that particular child. One cheat is to set the policy to 'ALL', but the better way is to actually listen for the FlexEvent.CONTENT_CREATION_COMPLETE instead (this is broadcast from the child of the navigation container).

例如:

<halo:ViewStack id="setupStack" width="100%" height="100%">
    <api:FileSelector width="100%" height="100%" owner="{this}" 
                      enumerationMode="{FileSystemEnumerationMode.DIRECTORIES_ONLY}"
                      hint="{networkDbAccessHint}" />
    <!- this is valid, but not it's children until contentCreateComplete is fired -->
    <api:DataImport width="100%" height="100%" owner="{this}" />
</halo:ViewStack>

'FileSelector' 和 'DataImport' 都广播该事件(扩展 s:NavigatorContent).

Both 'FileSelector' and 'DataImport' broadcast the event (extends s:NavigatorContent).