2016-05-10 89 views
-3

我用我的應用程序的大小調整處理程序來調整我的組件,但它拋出這個錯誤:「空對象引用」錯誤3

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> 
+1

什麼是錯誤? –

+0

你應該更具體一些,並解釋你不瞭解的空對象引用的哪一部分。 – csmckelvey

回答

-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); 
} 

希望能有所幫助。

+0

如果那個低估了我的帖子的人告訴我它有什麼問題,我將不勝感激。 – akmozo