2011-06-23 71 views
0

我正在将我的应用程序从flex 3升级到flex 4.5,并遇到错误。它发生在一个组件被动态添加到模块中。可以模拟错误的代码已经提取如下。Flex 4模块错误#1009

TestError.mxml

<?xml version="1.0" encoding="utf-8"?> 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"> 

<mx:Script> 
    <![CDATA[ 
     import mx.events.ModuleEvent; 

     protected function ml_readyHandler(event:ModuleEvent):void 
     { 
      trace("Module finished loading"); 
      (ml.child as TestErrorModuleInterface).testTracing(); 
     } 
    ]]> 
</mx:Script> 

<mx:ModuleLoader id="ml" url="TestErrorModule.swf" ready="ml_readyHandler(event)"/> 

</mx:Application> 

TestErrorModule.mxml

<?xml version="1.0" encoding="utf-8"?> 
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" 
     layout="absolute" width="100%" height="100%" 
     implements="TestErrorModuleInterface"> 
<mx:Canvas id="_box"> 
    <mx:Label text="HERE IS MY TEST MODULE" x="142" y="133"/> 
</mx:Canvas> 

<mx:Script> 
    <![CDATA[ 

     public function testTracing():void 
     { 
      trace("This is a method in the Test Module"); 
      var comp:TestErrorModuleComp = new TestErrorModuleComp(); 
      _box.addChild(comp); 
     } 
    ]]> 
</mx:Script> 

</mx:Module> 

TestErrorModuleInterface.as

package { 
public interface TestErrorModuleInterface { 
     function testTracing():void; 
    } 
} 

TestErrorModuleComp.mxml

<?xml version="1.0" encoding="utf-8"?> 
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"> 

    <mx:Label text="element" width="80" /> 

</mx:Canvas> 

这是详细的错误堆栈跟踪:

TypeError: Error #1009: Cannot access a property or method of a null object reference. 
at TestErrorModule/testTracing()[F:\MyEclipse_9_0\workspace\ViSR_Interface_Flex4\flex_src\TestErrorModule.mxml:16] 
at TestError/ml_readyHandler()[F:\MyEclipse_9_0\workspace\ViSR_Interface_Flex4\flex_src\TestError.mxml:12] 
at TestError/__ml_ready()[F:\MyEclipse_9_0\workspace\ViSR_Interface_Flex4\flex_src\TestError.mxml:17] 
at flash.events::EventDispatcher/dispatchEventFunction() 
at flash.events::EventDispatcher/dispatchEvent() 
at mx.core::UIComponent/dispatchEvent()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\UIComponent.as:13128] 
at mx.modules::ModuleLoader/moduleReadyHandler()[E:\dev\hero_private\frameworks\projects\mx\src\mx\modules\ModuleLoader.as:465] 
at flash.events::EventDispatcher/dispatchEventFunction() 
at flash.events::EventDispatcher/dispatchEvent() 
at ModuleInfoProxy/moduleEventHandler()[E:\dev\hero_private\frameworks\projects\framework\src\mx\modules\ModuleManager.as:1149] 
at flash.events::EventDispatcher/dispatchEventFunction() 
at flash.events::EventDispatcher/dispatchEvent() 
at ModuleInfo/readyHandler()[E:\dev\hero_private\frameworks\projects\framework\src\mx\modules\ModuleManager.as:793] 
at flash.events::EventDispatcher/dispatchEventFunction() 
at flash.events::EventDispatcher/dispatchEvent() 
at mx.core::FlexModuleFactory/update()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:535] 
at mx.core::FlexModuleFactory/docFrameHandler()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:681] 
at mx.core::FlexModuleFactory/docFrameListener()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:131] 

非常感谢!

回答

1

尝试使用类似:

<?xml version="1.0" encoding="utf-8"?> 
<mx:Module xmlns:mx="http://www.adobe.com/2006/mxml" 
     layout="absolute" width="100%" height="100%" 
     implements="TestErrorModuleInterface"> 
<mx:Canvas id="_box"> 
    <mx:Label text="HERE IS MY TEST MODULE" x="142" y="133"/> 
</mx:Canvas> 

<mx:Script> 
    <![CDATA[ 

     public function testTracing():void 
     { 
      trace("This is a method in the Test Module"); 
      if (initialized) 
       addComp(); 
      else 
       callLater(addComp); 
     } 

     private function addComp():void 
     { 
      var comp:TestErrorModuleComp = new TestErrorModuleComp(); 
      _box.addChild(comp); 
     } 
    ]]> 
</mx:Script> 

</mx:Module> 

的问题是你_box不是在模块装载时刻存在。您应该首先等待模块初始化。 callLater()这是一个丑陋的第一步。然后(在重构过程中)使用组件的失效效果会更好。

+0

谢谢,这正是问题的原因! – Jingwei