我正在尝试使用新的(ish)AS3全局错误处理类。我试图在Flex mxml应用程序中使用它。我根本无法工作。下面是一个显示问题的整个程序。我将其设置为使用Flash Player 10.2并使用Flex 4.5 SDK进行编译。
我尝试过使用Flex SDK 4.0和4.5,但在任何一种情况下都会出现错误。我必须在这里遗漏一些明显的东西。这是一个普通的Flex SWF文件,将显示在网页上。假设我可以导入UncaughtErrorEvent,我会做这样的事情来设置事件处理程序:
if(systemManager.loaderInfo.hasOwnProperty("uncaughtErrorEvents")) {
IEventDispatcher(
systemManager.loaderInfo["uncaughtErrorEvents"]).addEventListener(
"uncaughtError", uncaughtErrorHandler);
}
这一切看起来都非常糟糕,但我可以忍受,除了它不起作用!我在网上搜索过,找不到任何解释如何在我的上下文中完成这项工作的文档或示例。有什么建议吗?
完成计划:
<?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 :(得分: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)" />
简单的方法......
答案 1 :(得分:2)
从API(我建议您下次查看)中获取:
<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>