Adobe Air addEventListener未注册

Adobe Air addEventListener未注册,air,drag-and-drop,adobe,addeventlistener,Air,Drag And Drop,Adobe,Addeventlistener,对不起,我刚开始使用AdobeAIR,不知道为什么我的 addEventListener(NativeDragEvent.NATIVE\u DRAG\u ENTER,onDragIn) 及 addEventListener(NativeDragEvent.NATIVE\u拖放,onDragDrop) 电话线没有注册。它给了我一个错误,上面写着“调用可能未定义的方法addEventListener” 我正在尝试加载应用程序,并使用以下代码示例: 这是我的DragAndDropExampleClas

对不起,我刚开始使用AdobeAIR,不知道为什么我的

addEventListener(NativeDragEvent.NATIVE\u DRAG\u ENTER,onDragIn)

addEventListener(NativeDragEvent.NATIVE\u拖放,onDragDrop)

电话线没有注册。它给了我一个错误,上面写着“调用可能未定义的方法addEventListener”

我正在尝试加载应用程序,并使用以下代码示例:

这是我的DragAndDropExampleClass.as:


提前谢谢

在Mike Chamber的示例中,文件
DragAndDropExampleClass
中的代码旨在用作内联代码,而不是实际的类

因此,您删除了包/类指令。这应该已经解决了您遇到的问题(如注释跟踪中所述)

但是,在脚本标记
中包含内联代码是一种不好的做法,可能会导致混淆。我已将您的两个文件合并到下面的一个应用程序中。它与Mike Chambers的示例完全相同,只是在一个文件中。也许这会有帮助。我已经运行了这个应用程序,它可以正常工作

注意:我是用FlashBuilder4.6编译的,所以代码有点不同。如果您使用的是Flex3,请尝试像我一样结合Mike的两段代码

<?xml version="1.0" encoding="utf-8"?>
<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"
                       creationComplete="onCreationComplete()">
    <fx:Script>
        <![CDATA[
            import flash.desktop.ClipboardFormats;
            import flash.desktop.NativeDragManager;
            import flash.display.Sprite;
            import flash.events.NativeDragEvent;
            import flash.filesystem.File;
            import flash.filesystem.FileMode;
            import flash.filesystem.FileStream;

            //called when app has initialized and is about to display
            protected function onCreationComplete():void
            {
                //register for the drag enter event
                addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);

                //register for the drag drop event
                addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop);
            }

            //called when the user drags an item into the component area
            protected function onDragIn(e:NativeDragEvent):void
            {
                //check and see if files are being drug in
                if(e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
                {
                    //get the array of files
                    var files:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;

                    //make sure only one file is dragged in (i.e. this app doesn't
                    //support dragging in multiple files)
                    if(files.length == 1)
                    {
                        //accept the drag action
                        NativeDragManager.acceptDragDrop(this);
                    }
                }
            }

            //called when the user drops an item over the component
            protected function onDragDrop(e:NativeDragEvent):void
            {
                //get the array of files being drug into the app
                var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;

                //grab the files file
                var f:File = File(arr[0]);

                //create a FileStream to work with the file
                var fs:FileStream = new FileStream();

                //open the file for reading
                fs.open(f, FileMode.READ);

                //read the file as a string
                var data:String = fs.readUTFBytes(fs.bytesAvailable);

                //close the file
                fs.close();

                //display the contents of the file
                outputField.text = data;
            }
        ]]>
    </fx:Script>


    <mx:TextArea top="10" right="10" bottom="10" left="251"
                 id="outputField" />
    <mx:Text text="Drag a Text File into the Application"
             width="233" height="148" top="11" left="10"/>
</s:WindowedApplication>


谢谢你的帮助。我现在要调查这一切意味着什么!我刚刚编辑了我的答案。第一个是正确的(你的应用程序已经编译了),但是如果类仅仅是一个EventDispatcher,你的应用程序就不会运行:)你知道如何在我的类中标识我的outputField吗?在主应用程序中有一个id为=“outputField”的textArea,但它没有在我的DragAndDropExample类中注册。主应用程序在这里:好的,我很抱歉,我第一次点击Mike Chamber博客的链接时,花了这么长时间,我没有看到他的示例代码。我的回答不太正确。因为您在MXML中包含内联actionscript(DragAndDropExample),所以代码不应该在类中。它将成为你所学的任何课程的一部分。。。(编辑我的答案)我感谢你在这方面的持续帮助!我从DragAndDropExampleClass.as中取出了包{}和类{},现在我仍然不知道我在做什么。现在如何定义addEventListener?还是说我根本不需要这个DragAndDropExampleClass.as文件?这是DragAndDropExampleClass.as