获取错误类型 1061:通过静态类型的引用调用可能未定义的方法 addEventListener [英] Getting error Type 1061: Call to a possibly undefined method addEventListener through a reference with static type
本文介绍了获取错误类型 1061:通过静态类型的引用调用可能未定义的方法 addEventListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我将我的代码从我的应用程序移到一个单独的 AS 类文件,现在我收到以下错误:
I moved my code from my Application to a separate AS class file and now I'm getting the following errors:
1061: Call to a possibly undefined method addEventListener through a reference with static type Class.
1180: Call to a possibly undefined method addEventListener.
.
package managers {
import flash.events.Event;
import flash.events.EventDispatcher;
public class RemoteManager extends EventDispatcher {
public function RemoteManager() {
}
public static function init(clearCache:Boolean = false):void {
addEventListener(SETTINGS_CHANGE, settingChangeHandler);
addEventListener(ITEMS_UPDATED, itemsUpdatedHandler);
}
}
}
推荐答案
您的代码
addEventListener(SETTINGS_CHANGE, settingChangeHandler);
评估为
this.addEventListener(SETTINGS_CHANGE, settingChangeHandler);
在静态方法中没有 this
,因为它被设计为在没有实例的情况下运行.此外,您不能从静态类附加事件侦听器和调度事件.
There is no this
in a static method, since it's designed to function without an instance. Furthermore, you cannot attach an event listener and dispatch events from a static class.
将您的函数声明更改为
public function init(clearCache:Boolean = false):void
或者实现一个单例模式来获得一个静态类,它调度事件".
Or implement a singleton pattern to kinda get a "static class, that dispatches events".
这篇关于获取错误类型 1061:通过静态类型的引用调用可能未定义的方法 addEventListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文