【问题标题】:Flash TypeError: Error #1009: Cannot access a property or method of a null object reference. - when accessing TLFTextField from Document ClassFlash TypeError:错误 #1009:无法访问空对象引用的属性或方法。 - 从文档类访问 TLFTextField 时
【发布时间】:2011-12-10 11:45:38
【问题描述】:

我迷路了。当我的文档类第一次尝试访问舞台上的简单文本字段(从 IDE 添加,而不是 actionscript)时,我收到了 TypeError: Error #1009: Cannot access a property or method of a null object reference. 输出消息

package  {

import flash.display.*;
import fl.text.*;
import flash.text.*;
import flash.events.*;
import flash.net.*;

public class Main extends MovieClip {

    private var _netConnection:NetConnection;
    private var _responder:Responder;
    /* some other public + private vars */

    public function Main() {
        init();
    }

    public function init(e:*=null):void {
        _netConnection = new NetConnection();
        _responder = new Responder(uponResult);

        txt.text = "init()";
    }
    /* more functions */
  }
}

我尝试添加 txt.addEventListener(Event.ENTER_FRAME, init); 以防 txt TLFTextField 在开始时不...那里...,但它仍然输出错误。

我觉得自己有点像个白痴atm,预后文件是什么? JB

【问题讨论】:

  • 这取决于您初始化txt 的方式和位置。你能发布代码吗?
  • txt 是通过 Flash IDE 手动添加到舞台的,而不是通过 actionscript。
  • 如果 txt 不存在,您也无法为其添加事件侦听器...您是否尝试过在文档类的 Event.ADDED_TO_STAGE 上调用 init?
  • 我现在做到了,谢谢 weltraumpirat 和 heartcode :)

标签: actionscript-3 flash-cs5 typeerror document-class


【解决方案1】:

TLFTextFields 是奇怪的生物,我最近遇到了很多问题。

我会尝试使用 Event.ADDED_TO_STAGE 事件,因为当您尝试访问 TLFTextField 时,它们必须在舞台上:

public function Main() {
  addEventListener(Event.ADDED_TO_STAGE, init);
};
public function init(e:Event):void {
  removeEventListener(Event.ADDED_TO_STAGE, init);
  txt.text = "init()";
};

如果您的 TLFTextField 位于主时间轴的第一帧,它应该可以工作。

如果这个有魔法,请告诉我,

罗伯

【讨论】:

  • 成功了,Rob,非常感谢!让我难过的另一件事是 TLFTextFields 似乎只有在可选择或可编辑时才能正常工作,而不是只读时。这是一个动作脚本错误,还是您需要做额外的事情才能使其正常工作?
  • TLFTextField 仍然是一个测试版功能,不幸的是充满了错误。我想说尽可能多地阅读它,如果你的代码被卡住了,回到这里:)
猜你喜欢
  • 2013-05-16
  • 2012-12-29
  • 1970-01-01
  • 2015-05-27
  • 2014-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多