【问题标题】:"null object reference" error in ActionScript 3ActionScript 3 中的“空对象引用”错误
【发布时间】:2016-05-10 14:52:44
【问题描述】:

我使用我的应用程序的调整大小处理程序来调整我的组件的大小,但它抛出了这个错误:

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

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
           resize="application2_resizeHandler(event)" >
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;
            import mx.events.ResizeEvent;

            private var employeeName:String = 'ravi';

            protected function application2_resizeHandler(event:ResizeEvent):void
            {
                mainGroup.width = stage.width - 10;
                mainGroup.x = 5;                    
            }

        ]]>
    </fx:Script>    
    <s:Group width="100%" height="100%">
        <s:VGroup id="mainGroup" >
            <s:Label id="employeeNameLabel" text="{employeeName}" />
            <s:Label id="departmentLabel"  />
        </s:VGroup>

        <s:Button id="getData" />
    </s:Group>
</s:Application>

【问题讨论】:

  • 你应该更具体地解释你不理解空对象引用的哪一部分。

标签: actionscript-3 flash apache-flex flex4


【解决方案1】:

您收到 #1009 错误,因为您的 resize 事件在创建对象之前被触发。因此,您应该等待,并且您的应用程序被添加到阶段以便能够使用阶段对象。

为此,我认为最好的事件是applicationComplete 事件,然后您可以添加一个resize 事件来调整组件的大小...

所以你可以这样做,例如:

<s:Application 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"
    applicationComplete="application2_applicationCompleteHandler(event)" >

然后

protected function application2_applicationCompleteHandler(event:FlexEvent):void
{
    // if you want you can resize your component for the 1st time
    // by calling the application2_resizeHandler() function
    application2_resizeHandler(new ResizeEvent(ResizeEvent.RESIZE));

    // add the resize event listener to your app
    event.target.addEventListener(ResizeEvent.RESIZE, application2_resizeHandler);
}

希望能有所帮助。

【讨论】:

  • 如果对我的帖子投反对票的人告诉我它有什么问题,我将不胜感激。
猜你喜欢
  • 1970-01-01
  • 2018-09-15
  • 2014-03-25
  • 1970-01-01
  • 1970-01-01
  • 2013-12-05
  • 1970-01-01
  • 2015-09-13
  • 1970-01-01
相关资源
最近更新 更多