我正在将我的应用程序从flex 3升级到flex 4.5并遇到错误。将组件动态添加到模块中时发生了这种情况。可以模拟错误的代码如下所示。
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]
非常感谢!
答案 0 :(得分: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()
这是一个丑陋的第一步。最好使用组件的失效(在重构过程中)。