【问题标题】:Type Coercion failed in 'loader' and 'loaded' applications类型强制在“加载器”和“加载”应用程序中失败
【发布时间】:2014-04-20 19:03:52
【问题描述】:

我的主应用程序 swf 文件正在由一个简单的加载程序 swf 应用程序加载,该加载程序对破坏缓存很有用。在这两个应用程序中,我都希望能够访问单例。我将为您提供一个重现该错误的示例。所以这是我们的单例:

package {
    public class SingletonTest {
        public static const instance:SingletonTest = new SingletonTest();
        public function SingletonTest() { /* Nothing here. */ }
        public function test():void {
            trace("SingletonTest!");
        }
    }
}

在我调用的加载器和主类的构造函数中:

SingletonTest.instance.test();

这样做是为了确保我的单例类代码将包含在两个应用程序中。我不会为您提供加载程序代码,但它非常简单。它创建 Loader 实例,它创建 LoaderContext 为它提供当前的 ApplicationDomain 和 SecurityDomain 等...

但是当我启动加载器应用程序时,出现以下错误:

Main Thread (Suspended: TypeError: Error #1034: Type Coercion failed: 
cannot convert SingletonTest@47a3091 to SingletonTest.) 
    SingletonTest$cinit 

在使用加载程序加载主应用程序后,我立即收到此错误。 Event.COMPLETE 尚未分派,因此不涉及任何处理程序。我花了很多时间试图找到有关应用程序或安全域的信息,但这似乎不是问题,因为我接下来发现的内容真的很奇怪。如果改为:

public static const instance:SingletonTest = new SingletonTest();

我会写:

private static var _instance:SingletonTest;
public static function get instance():SingletonTest {
    if (_instance == null) _instance = new SingletonTest();
    return _instance;
}

那么就不会有这样的错误,一切都会好起来的。显然,flash player 在这里执行了一些意想不到的行为。关于这个问题的信息很少。大多数人因为缺少加载程序上下文而收到此错误,但正如我之前所说的,情况并非如此。对此的一些讨论甚至没有任何答案,这对我来说很奇怪,因为我发现这是使用小型应用程序加载器时遇到的一个很常见的问题。

【问题讨论】:

    标签: actionscript-3 flash loader applicationdomain securitydomain


    【解决方案1】:

    花了将近 12 个小时试图解决这个问题。终于找到了解决办法。为了避免我上面提供的运行时错误,您需要将单例类编译到 swc-library。而已。

    在我的例子中,单例提供了对日志记录的全局访问,唯一的选择是从单例模式切换到静态类。但我并不太想要它,因为我真的很喜欢这种单行实例化:“public static cosnt instance:MyClass = new MyClass();”。

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      相关资源
      最近更新 更多