解决 Flex4 Error in Module 的有关问题

解决 Flex4 Error in Module 的问题

 

首先我们先看一下错误信息:

 

 

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at mx.styles::StyleProtoChain$/initProtoChainForUIComponentStyleName()[E:\dev\4.x\frameworks\projects\framework\src\mx\styles\StyleProtoChain.as:356]
    at mx.styles::StyleProtoChain$/initProtoChain()[E:\dev\4.x\frameworks\projects\framework\src\mx\styles\StyleProtoChain.as:171]
    at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::initProtoChain()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:10446]
    at mx.core::UIComponent/regenerateStyleCache()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:10509]
    at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::addingChild()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7213]
    at mx.core::UIComponent/addChild()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:6910]
    at mx.controls.listClasses::ListBase/createChildren()[E:\dev\4.x\frameworks\projects\framework\src\mx\controls\listClasses\ListBase.as:3775]
    at mx.controls::DataGrid/createChildren()[E:\dev\4.x\frameworks\projects\framework\src\mx\controls\DataGrid.as:1133]
    at mx.core::UIComponent/initialize()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7349]
    at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7241]
    at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:3972]
    at mx.core::Container/addChildAt()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:2616]
    at mx.core::Container/addChild()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:2534]
    at mx.core::Container/createComponentFromDescriptor()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:4392]
    at mx.core::Container/createComponentsFromDescriptors()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:4181]
    at mx.core::Container/createChildren()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:3187]
    at mx.core::UIComponent/initialize()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7349]
    at mx.core::Container/initialize()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:3129]
    at 你对应的模块::/initialize()
    at mx.core::UIComponent/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\UIComponent.as:7241]
    at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::childAdded()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:3972]
    at mx.core::Container/addChildAt()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:2616]
    at mx.core::Container/addChild()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\Container.as:2534]
    at mx.modules::ModuleLoader/moduleReadyHandler()[E:\dev\4.x\frameworks\projects\framework\src\mx\modules\ModuleLoader.as:468]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at ModuleInfoProxy/moduleEventHandler()[E:\dev\4.x\frameworks\projects\framework\src\mx\modules\ModuleManager.as:1168]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at ModuleInfo/readyHandler()[E:\dev\4.x\frameworks\projects\framework\src\mx\modules\ModuleManager.as:812]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at mx.core::FlexModuleFactory/update()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:433]
    at mx.core::FlexModuleFactory/docFrameHandler()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:582]
    at mx.core::FlexModuleFactory/docFrameListener()[E:\dev\4.x\frameworks\projects\framework\src\mx\core\FlexModuleFactory.as:126]
 

我还没搞清楚Flex4的Module机制究竟在搞什么,以下是adobe论坛的原话 http://forums.adobe.com/thread/831894

 

 

In your main application do not reference the module's class name. It looks like you are referencing a module "sample" on this line:

 private var mod:sample;

If you need to call methods on the module use an interface to provide the API. Referencing the module's class name causes all the classes in the module to be linked into the main application, defeating the purpose of modules.

 

 

译文:

 

 

总之你不能在你的被优化的application当中去引用你自定义的Module模块,任何的引用都将会导致在Module里面的所有类会被链接到application当中。你只定义一个通用的api来访为这些模块。这本来就是模块开发的原则。
 

 

其实我们在实际开发中千万不要去直接引用module,也不要去new,我们应该去定义一个全局的变量,然后通过它去操作module里面的属性和方法。