2011-05-16 83 views
1

我正在尝试使用新的(ish)AS3全局错误处理类。我试图在Flex mxml应用程序中使用它。我无法让它工作。以下是显示问题的完整程序。我将其设置为使用Flash Player 10.2并使用Flex 4.5 SDK进行编译。Flex 4.0/4.5全局错误处理

我已经尝试过使用Flex SDK 4.0和4.5,但在任何情况下都会出现错误。我必须在这里忽略一些明显的东西。这是一个普通的Flex SWF文件,将在网页上显示。假设我可以导入UncaughtErrorEvent,然后,我会做这样的事情来设置事件处理程序:

if(systemManager.loaderInfo.hasOwnProperty("uncaughtErrorEvents")) {     
     IEventDispatcher(
      systemManager.loaderInfo["uncaughtErrorEvents"]).addEventListener(
       "uncaughtError", uncaughtErrorHandler); 
    } 

这一切似乎可怕的缺憾,但我可以忍受的不同之处在于它不工作!我搜索了网页,找不到任何文档或示例来解释如何在我的上下文中进行此项工作。有什么建议?

完整的程序:从API(我建议你通过下一次看)

<?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" applicationComplete="onApplicationComplete();" 
        > 
     <fx:Declarations> 
      <!-- Place non-visual elements (e.g., services, value objects) here --> 
     </fx:Declarations> 
     <fx:Script> 
      <![CDATA[ 
       private function onApplicationComplete() : void { 
        systemManager.loaderInfo.uncaughtErrorEvents.addEventListener("uncaughtError", uncaughtErrorHandler); 
       } 
       private function uncaughtErrorHandler(event:Event) : void { 
        trace(event.toString()); 
       } 
      ]]> 
     </fx:Script> 
     <s:Button x="153" y="64" label="Trigger Error" id="triggerButton" click="throw new Error('myError')"/> 
    </s:Application> 
+0

尝试使用stage.loaderInfo而不是systemManager? http://stackoverflow.com/questions/3315904/global-error-handler-for-flash-player-10-1-not-working – Satish 2011-05-16 23:17:29

+0

嗯,那个线程只是确认我遇到的问题。无论我使用哪个loaderInfo addEventListener,它都不起作用。 – 2011-05-17 11:22:10

回答

2

采取:

<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/halo" 
         applicationComplete="applicationCompleteHandler();"> 

    <fx:Script> 
     <![CDATA[ 
      import flash.events.ErrorEvent; 
      import flash.events.MouseEvent; 
      import flash.events.UncaughtErrorEvent; 

      private function applicationCompleteHandler():void 
      { 
       loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler); 
      } 

      private function uncaughtErrorHandler(event:UncaughtErrorEvent):void 
      { 
       if (event.error is Error) 
       { 
        var error:Error = event.error as Error; 
        // do something with the error 
       } 
       else if (event.error is ErrorEvent) 
       { 
        var errorEvent:ErrorEvent = event.error as ErrorEvent; 
        // do something with the error 
       } 
       else 
       { 
        // a non-Error, non-ErrorEvent type was thrown and uncaught 
       } 
      } 

      private function clickHandler(event:MouseEvent):void 
      { 
       throw new Error("Gak!"); 
      } 
     ]]> 
    </fx:Script> 

    <s:Button label="Cause Error" click="clickHandler(event);"/> 
</s:WindowedApplication> 
+0

哇,没什么更糟的是,粗鲁的回应!很明显,我确实阅读过文档,因为我的代码是直接从他们中删除的。这是行不通的。正如你从其他线索看到的,我并不孤单。我在阅读API文档并尝试了至少5个来自各种博客的其他建议后才发布。功能或所有文档都有问题。我有一个30行完整的程序,显示这不起作用。 – 2011-05-17 11:23:40

+0

我很好奇:你有没有试过你的代码?首先,它没有编译。我不得不将WindowedApplication改为Application。然后我必须将目标FP设置为10.1。然后它编译,但给出了我的代码的确切结果。你的程序中也存在逻辑错误。它需要在错误处理程序中调用event.preventDefault()。所以我认为你对文档过分信任,因为我尝试了每个我能找到的例子,而且没有按预期工作。我仍然试图弄清楚为什么你的结论是我没有阅读API文档,因为我的例子几乎与API参考文献中的相同。 – 2011-05-17 11:34:50

+0

粗鲁的回应?我只是说你应该看看API。如果你做了,对你有好处,但你没有提到它或显示相同的代码。是的,我确实尝试过,如果你遵循文档(WindowedApplication是Air,API说minium FP是10.1),那么它工作得很好。它不需要*来调用preventDefault(),如果你不想让一个错误框弹出,那就是一个附加值。但是,这里显示的例子完美地工作。 – 2011-05-17 12:02:07

3
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
          xmlns:s="library://ns.adobe.com/flex/spark" 
          xmlns:mx="library://ns.adobe.com/flex/mx" 
          uncaughtError="uncaughtErrorHandler(event)" /> 

最简单的方法...