【问题标题】:AS3 Error #1007: Instantiation attempted on a non-constructorAS3 错误 #1007:在非构造函数上尝试实例化
【发布时间】:2011-08-12 07:30:38
【问题描述】:

我在尝试创建库项目的实例时遇到标题错误:

trace(ApplicationSettings.AGEGATEVIEW);
var clazz:Class = ApplicationSettings.AGEGATEVIEW as Class;
ageGateForm = new clazz();

我的跟踪正确地输出了也是库链接的类名(即 com.age.AgegateInputView)。我想我遗漏了一些明显的东西......

编辑: ApplicationSettings.AGEGATEVIEW 所指的类:

package com
{
    import com.AgegateSubmitAgeEvent;
    import com.ButtonEvent;
    import com.LabelButton;
    import flash.text.TextFormat;
    import flash.events.FocusEvent;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.display.MovieClip;

    public class AgegateInputView extends MovieClip
    {
        protected var startColor         :uint = 0xCCCCCC;
        protected var textColor          :uint = 0x000000;
        protected var errorColor         :uint = 0xFF0000;

        protected var startFormat        :TextFormat;
        protected var textFormat         :TextFormat;
        protected var errorFormat        :TextFormat;

        protected var daySafe            :Boolean = false;
        protected var monthSafe          :Boolean = false;
        protected var yearSafe           :Boolean = false;


        public var dayInput              :TextField;
        public var monthInput            :TextField;
        public var yearInput             :TextField;

        public var submitBut             :LabelButton;

        public function AgegateInputView():void
        {
            startFormat = new TextFormat();
            startFormat.color = startColor;

            textFormat = new TextFormat();
            textFormat.color = textColor;

            errorFormat = new TextFormat();
            errorFormat.color = errorColor;

            if(stage) {
                init();
            } else {
                addEventListener(Event.ADDED_TO_STAGE,init);
            }
        }

        protected function setupInputs():void
        {
            dayInput.defaultTextFormat = startFormat;
            dayInput.text = "DD";
            dayInput.restrict = "0-9";
            dayInput.maxChars = 2;

            monthInput.defaultTextFormat = startFormat;
            monthInput.text = "MM";
            monthInput.restrict = "0-9";
            monthInput.maxChars = 2;

            yearInput.defaultTextFormat = startFormat;
            yearInput.text = "YYYY";
            yearInput.restrict = "0-9";
            yearInput.maxChars = 4;
        }

        protected function setupButton():void
        {
            submitBut.dispatchObj = {type:"age_submit"};
            submitBut.enabled = true;
        }

        protected function addListeners():void
        {
            dayInput.addEventListener(FocusEvent.FOCUS_IN, onInputFocus);
            dayInput.addEventListener(FocusEvent.FOCUS_OUT,checkDay);
            monthInput.addEventListener(FocusEvent.FOCUS_IN, onInputFocus);
            monthInput.addEventListener(FocusEvent.FOCUS_OUT,checkMonth);
            yearInput.addEventListener(FocusEvent.FOCUS_IN, onInputFocus);
            yearInput.addEventListener(FocusEvent.FOCUS_OUT,checkYear);
            submitBut.addEventListener(ButtonEvent.BUTTON_CLICK, onSubmit);
        }

        protected function removeListeners():void
        {
            dayInput.removeEventListener(FocusEvent.FOCUS_IN, onInputFocus);
            dayInput.removeEventListener(FocusEvent.FOCUS_OUT,checkDay);
            monthInput.removeEventListener(FocusEvent.FOCUS_IN, onInputFocus);
            monthInput.removeEventListener(FocusEvent.FOCUS_OUT,checkMonth);
            yearInput.removeEventListener(FocusEvent.FOCUS_IN, onInputFocus);
            yearInput.removeEventListener(FocusEvent.FOCUS_OUT,checkYear);
            submitBut.removeEventListener(ButtonEvent.BUTTON_CLICK, onSubmit);
        }

        protected function init(evt:Event = null):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);
            setupInputs();
            setupButton();
            addListeners();
        }

        protected function onInputFocus(evt:FocusEvent):void
        {
            var tf:TextField = evt.target as TextField;
            if(isNaN(Number(tf.text))) tf.text = "";
            tf.setTextFormat(textFormat);
            tf.defaultTextFormat = textFormat;
        }

        protected function checkDay(evt:FocusEvent):void
        {
            var value:Number = Number(dayInput.text);
            if(isNaN(value) || value < 1 || value > 31) {
                dayInput.setTextFormat(errorFormat);
                daySafe = false;
            } else {
                daySafe = true;
            }
        }

        protected function checkMonth(evt:FocusEvent):void
        {
            var value:Number = Number(monthInput.text);
            if(isNaN(value) || value < 1 || value > 12) {
                monthInput.setTextFormat(errorFormat);
                monthSafe = false;
            } else {
                monthSafe = true;
            }
        }

        protected function checkYear(evt:FocusEvent):void
        {
            var value:Number = Number(yearInput.text);
            var date:Date = new Date();
            if(isNaN(value) || value < 1900 || value > date.getFullYear()) {
                yearInput.setTextFormat(errorFormat);
                yearSafe = false;
            } else {
                yearSafe = true;
            }
        }

        protected function onSubmit(evt:ButtonEvent):void
        {
            evt.stopPropagation();
            if(daySafe && monthSafe && yearSafe) {
                dispatchEvent(new AgegateSubmitAgeEvent(AgegateSubmitAgeEvent.SUBMIT_AGE,{_day:dayInput.text, _month:monthInput.text, _year:yearInput.text}));
            }
        }
    }
}

所有公共属性都是组件舞台上的项目。该组件在舞台上测试时工作正常,只是动态附加它失败了。

【问题讨论】:

  • 你能告诉我们 ApplicationSettings.AGEGATEVIEW 源代码吗?
  • ApplicationSettings.AGEGATEVIEW 是对类的字符串引用。我已经用代码更新了我的问题。

标签: flash actionscript-3 typeerror


【解决方案1】:

在您的 ApplicationSettings.AGEGATEVIEW 类中,您是否尝试像这样填充数组:

new Array[1, 2, 3];

而不是正确的方式:

new Array(1, 2, 3);

【讨论】:

  • 顺便说一句,如果你知道这些项目,最好的方法是var a:Array = [1, 2, 3];
【解决方案2】:

真的很蠢,忘了用getDefinition

var clazz:Class =  ApplicationDomain.currentDomain.getDefinition(ApplicationSettings.AGEGATEVIEW) as Class;

【讨论】:

    猜你喜欢
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-16
    • 1970-01-01
    相关资源
    最近更新 更多