AIR 应用程序:无法访问空对象引用的属性或方法。不知道是什么原因造成的

发布于 2024-11-09 04:11:10 字数 19022 浏览 0 评论 0原文

我正在使用开源 MP3 播放器 Howler 的代码,并尝试将其移植到 Spark MobileApplication 类型。我收到一个空指针异常,但我不知道是什么原因导致的。我已经尝试在我认为导致错误的地方使用断点进行广泛的调试,并在未触及的 Howler 项目中设置断点,但范围内的所有变量在我的非工作项目和 Howler 项目之间似乎都是相同的。我唯一能想到的是Howler使用MX组件而我使用spark。我已将所有代码粘贴在下面(非常长),但我已将引发错误的行加粗。当我在浏览文件夹对话框中选择一个文件夹后,立即发生错误。

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="Home"
        xmlns:Comp="components.*" xmlns:display="flash.display.*">
    <s:Button id="browse" x="546" y="43" label="Open Directory" click="browseForFolder()"/>
    <s:DataGrid id="dgPlaylist" width="82" height="141" itemRenderer="components.DurationFormatter">
    </s:DataGrid>
    <s:Button id="btnForward" x="187" y="126" label="Forward"/>
    <s:Button id="btnPause" x="90" y="39" label="Pause"/>
    <s:Button id="btnBack" x="55" y="166" label="Back" click="changeSoundIndex(-1)"/>
    <s:Button id="btnPlay" x="336" y="199" label="Button"/>
    <s:Button id="btnStop" x="366" y="89" label="Stop"/>
    <s:VScrollBar id="sldrPosition" x="280" y="43" mouseDown="thumbTimer.stop()" 
                  mouseUp="thumbTimer.start()"
                  />
    <s:VScrollBar id="sldrVolume" x="265" y="234" change="ChangeSoundTransform()" 
                  />
    <s:RichText id="txtID3" x="236" y="41" width="99">
    </s:RichText>

    <fx:Declarations>

    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import com.ericcarlisle.PlayList;
            import com.ericcarlisle.PlayModes;
            import com.ericcarlisle.Utility;

            import flash.desktop.NativeDragManager;
            import flash.media.Sound;

            import mx.core.UIComponent;
            import mx.events.CloseEvent;
            import mx.events.DragEvent;

            import org.osmf.traits.PlayTrait;

            import spark.components.DataGrid;


            // Player properties
            private var playMode:String = "STOP";
            private var volume:uint;
            private const panning:int = 0;

            private var selectedFileCount:uint;
            private var loadedFileCount:uint;
            private var soundIndex:uint;

            // Player objects
            private var SoundObj:Sound;
            private var Channel:SoundChannel;
            private var Transform:SoundTransform;           
            private var thumbTimer:Timer;
            private var PlayList:com.ericcarlisle.PlayList;
            private var SoundFilter:FileFilter = new FileFilter("Sounds", "*.mp3;*.wav");
            //private var PlaylistFilter:FileFilter = new FileFilter("Sounds", "*.pls;*.m3u");

            // Visualization objects.
            private var Spectrum:ByteArray;
            private const VISUALIZER_HEIGHT:Number = 50;
            private const VISUALIZER_COLOR:Number = 0x336699;


            // ID3 and other metadata
            private var ID3:ID3Info;
            private var Duration:int;

            /*---------- PLAYER INITIALIZER ----------*/

            // Initialization function used to add event handlers and set initial settings.
            private function init():void
            {
                // Set player initial settings.
                playMode = PlayModes.STOP;  
                selectedFileCount = 0;
                loadedFileCount = 0;
                soundIndex = 0;

                // Set initial application height.
                //this.height= cvsControlBar.height + cvsPlayer.height;

                // Set volume.
                volume = sldrVolume.value;

                // Instantiate sound objects.
                Channel = new SoundChannel();
                Transform = new SoundTransform(volume/100, panning);
                PlayList = new com.ericcarlisle.PlayList(); 

                // Bind playlist data to datagrid.
                dgPlaylist.dataProvider = PlayList.Sounds;

                // Create a timer to control the song position hslider.             
                thumbTimer = new Timer(500);
                thumbTimer.addEventListener(TimerEvent.TIMER, onTimerTick);

                // Create event handlers for application.
                this.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
                this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

                this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onPlayerDragInto);
                this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onPlayerDropInto);      
                this.addEventListener(InvokeEvent.INVOKE, onInvoke);

            }

            /*---------- DRAG/DROP & FILE MANAGEMENT ----------*/

            private function onInvoke(event:InvokeEvent):void
            {
                if (event.arguments.length > 0)
                {
                    var file:File;
                    var files:Array = new Array();

                    for (var i:int = 0; i < event.arguments.length; i++)
                    {
                        file = new File(event.arguments[i]);    
                        files.push(file);
                    }

                    if (PlayList.Sounds.length > 0) removeAllSounds();
                    loadFiles(files);
                }
            }


            // Handles file selection event dispatched by browse dialog.
            private function onFileSelect(event:FileListEvent):void
            {
                loadFiles(event.files);
            }

            // Handles folder selection event dispatched by browse dialog.
            private function onDirectorySelect(event:Event):void
            {
                var directory:File = event.target as File;
                **loadFiles(directory.getDirectoryListing());**
            }

            // Loads a batch of files into the playlist.
            private function loadFiles(files:Array):void
            {           

                var file:File;

                // Count the number of files selected.  Only accept files with .mp3 extension.
                selectedFileCount = 0;

                for (var i:uint = 0; i < files.length; i++)
                {
                    file = files[i];
                    if (file.extension == "mp3") selectedFileCount++;
                }

                // Reset the count on files currently loaded.
                loadedFileCount = 0;

                **// Set the player mode so that loaded files are played automatically.
                if (PlayList.Sounds.length == 0) playMode = PlayModes.LOADTOPLAY;**

                // Load files as sound objects.             
                for(var j:uint = 0; j < files.length; j++)
                {
                    file = files[j];
                    if (file.extension == "mp3" || file.extension == "wav")
                    {
                        var sound:Sound = new Sound(new URLRequest(file.url));
                        sound.addEventListener(Event.ID3,onID3);
                    }
                }
            }

            // Presents file browse (multiple file) dialog.
            private function browseForFiles():void
            {
                var SoundFile:File = new File();
                SoundFile.browseForOpenMultiple("Open", [SoundFilter]);//, PlaylistFilter]);
                SoundFile.addEventListener(FileListEvent.SELECT_MULTIPLE, onFileSelect);
            }

            // Presents file browse (folder) dialog.
            private function browseForFolder():void
            {
                var directory:File = File.documentsDirectory;
                directory.browseForDirectory("Select Directory");
                directory.addEventListener(Event.SELECT, onDirectorySelect);
            } 

            // Accept files dragged into player.
            private function onPlayerDragInto(event:Event):void
            {
                NativeDragManager.acceptDragDrop(this);
            }   

            // Manages files dropped into player.
            private function onPlayerDropInto(event:NativeDragEvent):void
            {
                // Accept only files.
                if (event.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
                {
                    // Parse dragged contents into array of files.
                    var dragFiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;

                    // Load the files.
                    loadFiles(dragFiles);
                }
            }   

            /*---------- SOUND MANAGEMENT ----------*/

            private function loadSound():void
            {
                SoundObj = new Sound();
                SoundObj.load(new URLRequest(PlayList.Sounds[soundIndex]["url"]));
                SoundObj.addEventListener(Event.COMPLETE, onSoundLoaded);
            }

            private function onSoundLoaded(event:Event):void
            {
                // Retrieve data for current sound from playlist.
                var soundData:Object = PlayList.Sounds[soundIndex];

                // Place ID3 information into the readout panel.
                //txtID3.htmlText = Utility.ReadoutHTML(soundData["title"], soundData["track"], soundData["album"], soundData["artist"], soundData["year"], soundData["duration"]);

                // Configure the holizontal slider to act as a playhead.
                sldrPosition.maximum = soundData["duration"];

                // Set the selected row in the playlist display.
                dgPlaylist.selectedIndex = soundIndex;

                // Start the player if the mode is correct.
                if (playMode == PlayModes.LOADTOPLAY)
                {
                    playSound();
                }
                else
                {
                    playMode = PlayModes.LOADED;                    
                }

            }

            // Plays the current sound.
            public function playSound():void
            {                   
                // Load sound into channel.
                Channel.stop();
                Channel = SoundObj.play(sldrPosition.value,0,Transform);
                playMode = PlayModes.PLAY;

                // Start position timer.
                thumbTimer.start();

                // Configure UI controls.
                btnPlay.visible = false;
                btnPause.visible = true;
                sldrPosition.enabled = true;
                btnPlay.enabled = true;
                btnStop.enabled = true;
                setBackForwardButtons();

                Channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
            }       

            private function setBackForwardButtons():void
            {
                if (soundIndex == PlayList.Sounds.length-1 || PlayList.Sounds.length == 0)
                {
                    btnForward.enabled = false;
                }
                else
                {
                    btnForward.enabled = true;
                }

                if (soundIndex == 0 || PlayList.Sounds.length == 0)
                {
                    btnBack.enabled = false;
                }
                else
                {
                    btnBack.enabled = true;
                }
            }

            // Stops the current sound.
            public function stopSound():void
            {
                Channel.stop();             

                thumbTimer.stop();
                sldrPosition.value = 0;
                playMode = PlayModes.STOP;

                //Visualizer.graphics.clear();

                btnPlay.visible = true;
                btnPause.visible = false;
            }

            // Pause a playing sound.               
            private function pauseSound():void
            {
                Channel.stop();             
                thumbTimer.stop();
                btnPlay.visible = true;
                btnPause.visible = false;
            }

            // Change the sound index
            private function changeSoundIndex(delta:int):void
            {
                stopSound();
                playMode = PlayModes.LOADTOPLAY;
                soundIndex = soundIndex + delta;
                loadSound();
            }


            // Change the volume and panning via the sound transform object.
            private function ChangeSoundTransform():void
            {
                volume = Math.round(sldrVolume.value);
                Channel.soundTransform = new SoundTransform(volume/100, panning);
            }

            // Handles event for sound completing.
            private function onSoundComplete(event:Event):void
            {
                stopSound();
                soundIndex++;

                if (soundIndex < PlayList.Sounds.length)
                {
                    playMode = PlayModes.LOADTOPLAY;
                    loadSound();
                }

            }

            // Load ID3 information into local variables.  
            // Update the readout panel.
            // Configure position slider.
            private function onID3(event:Event):void
            {
                var sound:Sound = Sound(event.target);
                ID3 = ID3Info(sound.id3);
                Duration = Math.floor(sound.length);

                // Load sound id3 data into the playlist.
                PlayList.AddSound(ID3.songName, ID3.album, ID3.artist, ID3.track, ID3.year, ID3.genre, Duration, sound.url);

                // Increment the loaded file count.
                loadedFileCount++;

                if (loadedFileCount == selectedFileCount * 2)
                {
                    // Refresh the playlist so that new results will be visually displayed.
                    PlayList.Sounds.refresh();

                    // Set the count properties.
                    selectedFileCount = 0;
                    loadedFileCount = 0;

                    soundIndex = 0;
                    if (playMode == PlayModes.LOADTOPLAY) loadSound();
                }

            }

            /*---------- VISUALIZATION ----------*/

            private function UpdateVisualizer():void
            {
                // Instantiate a new byte array to contain spectrum data.
                Spectrum = new ByteArray();

                // Clear the visualizer graphics.
                //Visualizer.graphics.clear();

                // Dump the spectrum data into the byte array.
                SoundMixer.computeSpectrum(Spectrum,false,0);

                var f:Number;
                var i:int;
                var ave:int;

                //Visualizer.graphics.lineStyle(1, VISUALIZER_COLOR,1);
                //Visualizer.graphics.beginFill(VISUALIZER_COLOR, 0.75);


                for (i = 0; i < 512; i=i+10) 
                {
                    f = Spectrum.readFloat();
                    //Visualizer.drawRoundRect(Math.floor(i*0.7) + 7, cvsReadout.height - 10, 4, -Math.abs(f) * (cvsReadout.height-10));
                }
                //Visualizer.graphics.endFill();

            }






            // Updates the position of the hslider thumb.
            private function onTimerTick(event:TimerEvent):void
            {
                sldrPosition.value = Math.round(Channel.position);
            }


            // Update the wave visualizer if the sound is playing.
            private function onEnterFrame(event:Event):void
            {
                if (playMode == PlayModes.PLAY)
                {
                    UpdateVisualizer();
                }
            }

            // Show application information.
            private function showHowlerInfo():void
            {
                //cvsAbout.visible = true;
            }

            private function togglePlayList():void
            {

            }

            private function onItemDoubleClick(event:Event):void
            {
                this.playMode = PlayModes.LOADTOPLAY;
                thumbTimer.stop();
                sldrPosition.value = 0;
                loadSound();
            }

            /*---------- ERROR HANDLING ----------*/
            // Handles IO errors.
            private function onIOError(event:IOErrorEvent):void
            {
                //Alert.show("File load error: " + event.text);
            }

            private function startMove():void
            {
                stage.nativeWindow.startMove();
            }

            private function unloadSound():void
            {
                stopSound();
                txtID3.text = "";
                btnPlay.visible = true;
                btnPause.visible = false;
                btnPlay.enabled = false;
                btnStop.enabled = false;
            }

            private function removeSound():void
            {
                var index:int = dgPlaylist.selectedIndex;

                if (index >= 0)
                {
                    if (index == soundIndex)
                    {
                        unloadSound();
                    }
                    PlayList.RemoveSoundAt(index);
                    PlayList.Sounds.refresh();
                    setBackForwardButtons();
                }
            }

            private function removeAllSounds():void
            {
                unloadSound();
                PlayList.Sounds.removeAll();
                PlayList.Sounds.refresh();
                setBackForwardButtons();
            }

            private function onKeyDown(event:KeyboardEvent):void
            {
                if (event.charCode.toString() == "8" || event.charCode.toString() == "127")
                {
                    removeSound();          
                }
            }
        ]]>
    </fx:Script>
</s:View>

我是 Flex 新手,所以我不知道这是否会导致问题,但 Howler 应用程序使用如下定义的 MX:DataGrid:

<mx:DataGrid x="6" 
                     y="7" 
                     width="388"
                     height="310"
                     id="dgPlaylist"
                     keyDown="onKeyDown(event)"
                     dragMoveEnabled="true"
                     doubleClickEnabled="true"
                     dragEnabled="true"
                     dropEnabled="true"
                     dragComplete="onPlaylistDragDrop(event)"
                     itemDoubleClick="onItemDoubleClick(event)">
            <mx:columns>
                <mx:DataGridColumn width="168" headerText="Title" dataField="title" />
                <mx:DataGridColumn width="160" headerText="Album" dataField="album"/>
                <mx:DataGridColumn width="60" headerText="Duration" dataField="duration" textAlign="right" itemRenderer="components.DurationFormatter"/>
            </mx:columns>
        </mx:DataGrid>

它有我没有使用的附加列。这可能是原因吗?

I'm using code for the open source MP3 player Howler and trying to port it to a Spark MobileApplication type. I'm getting a null pointer exception and I have no idea what's causing it. I've tried debugging extensively with breakpoints at what I think is causing the error, and set breakpoints in the untouched Howler project but all the variables in scope seem to be identical between my non-working project, and the Howler project. The only thing I can think of is that Howler uses MX components and I am using spark. I've pasted all my code below (which is very long) but I've bolded the lines that are throwing the error. The error occurs immediately after I choose a folder in the browse folder dialog.

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" title="Home"
        xmlns:Comp="components.*" xmlns:display="flash.display.*">
    <s:Button id="browse" x="546" y="43" label="Open Directory" click="browseForFolder()"/>
    <s:DataGrid id="dgPlaylist" width="82" height="141" itemRenderer="components.DurationFormatter">
    </s:DataGrid>
    <s:Button id="btnForward" x="187" y="126" label="Forward"/>
    <s:Button id="btnPause" x="90" y="39" label="Pause"/>
    <s:Button id="btnBack" x="55" y="166" label="Back" click="changeSoundIndex(-1)"/>
    <s:Button id="btnPlay" x="336" y="199" label="Button"/>
    <s:Button id="btnStop" x="366" y="89" label="Stop"/>
    <s:VScrollBar id="sldrPosition" x="280" y="43" mouseDown="thumbTimer.stop()" 
                  mouseUp="thumbTimer.start()"
                  />
    <s:VScrollBar id="sldrVolume" x="265" y="234" change="ChangeSoundTransform()" 
                  />
    <s:RichText id="txtID3" x="236" y="41" width="99">
    </s:RichText>

    <fx:Declarations>

    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            import com.ericcarlisle.PlayList;
            import com.ericcarlisle.PlayModes;
            import com.ericcarlisle.Utility;

            import flash.desktop.NativeDragManager;
            import flash.media.Sound;

            import mx.core.UIComponent;
            import mx.events.CloseEvent;
            import mx.events.DragEvent;

            import org.osmf.traits.PlayTrait;

            import spark.components.DataGrid;


            // Player properties
            private var playMode:String = "STOP";
            private var volume:uint;
            private const panning:int = 0;

            private var selectedFileCount:uint;
            private var loadedFileCount:uint;
            private var soundIndex:uint;

            // Player objects
            private var SoundObj:Sound;
            private var Channel:SoundChannel;
            private var Transform:SoundTransform;           
            private var thumbTimer:Timer;
            private var PlayList:com.ericcarlisle.PlayList;
            private var SoundFilter:FileFilter = new FileFilter("Sounds", "*.mp3;*.wav");
            //private var PlaylistFilter:FileFilter = new FileFilter("Sounds", "*.pls;*.m3u");

            // Visualization objects.
            private var Spectrum:ByteArray;
            private const VISUALIZER_HEIGHT:Number = 50;
            private const VISUALIZER_COLOR:Number = 0x336699;


            // ID3 and other metadata
            private var ID3:ID3Info;
            private var Duration:int;

            /*---------- PLAYER INITIALIZER ----------*/

            // Initialization function used to add event handlers and set initial settings.
            private function init():void
            {
                // Set player initial settings.
                playMode = PlayModes.STOP;  
                selectedFileCount = 0;
                loadedFileCount = 0;
                soundIndex = 0;

                // Set initial application height.
                //this.height= cvsControlBar.height + cvsPlayer.height;

                // Set volume.
                volume = sldrVolume.value;

                // Instantiate sound objects.
                Channel = new SoundChannel();
                Transform = new SoundTransform(volume/100, panning);
                PlayList = new com.ericcarlisle.PlayList(); 

                // Bind playlist data to datagrid.
                dgPlaylist.dataProvider = PlayList.Sounds;

                // Create a timer to control the song position hslider.             
                thumbTimer = new Timer(500);
                thumbTimer.addEventListener(TimerEvent.TIMER, onTimerTick);

                // Create event handlers for application.
                this.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
                this.addEventListener(Event.ENTER_FRAME, onEnterFrame);

                this.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onPlayerDragInto);
                this.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onPlayerDropInto);      
                this.addEventListener(InvokeEvent.INVOKE, onInvoke);

            }

            /*---------- DRAG/DROP & FILE MANAGEMENT ----------*/

            private function onInvoke(event:InvokeEvent):void
            {
                if (event.arguments.length > 0)
                {
                    var file:File;
                    var files:Array = new Array();

                    for (var i:int = 0; i < event.arguments.length; i++)
                    {
                        file = new File(event.arguments[i]);    
                        files.push(file);
                    }

                    if (PlayList.Sounds.length > 0) removeAllSounds();
                    loadFiles(files);
                }
            }


            // Handles file selection event dispatched by browse dialog.
            private function onFileSelect(event:FileListEvent):void
            {
                loadFiles(event.files);
            }

            // Handles folder selection event dispatched by browse dialog.
            private function onDirectorySelect(event:Event):void
            {
                var directory:File = event.target as File;
                **loadFiles(directory.getDirectoryListing());**
            }

            // Loads a batch of files into the playlist.
            private function loadFiles(files:Array):void
            {           

                var file:File;

                // Count the number of files selected.  Only accept files with .mp3 extension.
                selectedFileCount = 0;

                for (var i:uint = 0; i < files.length; i++)
                {
                    file = files[i];
                    if (file.extension == "mp3") selectedFileCount++;
                }

                // Reset the count on files currently loaded.
                loadedFileCount = 0;

                **// Set the player mode so that loaded files are played automatically.
                if (PlayList.Sounds.length == 0) playMode = PlayModes.LOADTOPLAY;**

                // Load files as sound objects.             
                for(var j:uint = 0; j < files.length; j++)
                {
                    file = files[j];
                    if (file.extension == "mp3" || file.extension == "wav")
                    {
                        var sound:Sound = new Sound(new URLRequest(file.url));
                        sound.addEventListener(Event.ID3,onID3);
                    }
                }
            }

            // Presents file browse (multiple file) dialog.
            private function browseForFiles():void
            {
                var SoundFile:File = new File();
                SoundFile.browseForOpenMultiple("Open", [SoundFilter]);//, PlaylistFilter]);
                SoundFile.addEventListener(FileListEvent.SELECT_MULTIPLE, onFileSelect);
            }

            // Presents file browse (folder) dialog.
            private function browseForFolder():void
            {
                var directory:File = File.documentsDirectory;
                directory.browseForDirectory("Select Directory");
                directory.addEventListener(Event.SELECT, onDirectorySelect);
            } 

            // Accept files dragged into player.
            private function onPlayerDragInto(event:Event):void
            {
                NativeDragManager.acceptDragDrop(this);
            }   

            // Manages files dropped into player.
            private function onPlayerDropInto(event:NativeDragEvent):void
            {
                // Accept only files.
                if (event.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
                {
                    // Parse dragged contents into array of files.
                    var dragFiles:Array = event.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;

                    // Load the files.
                    loadFiles(dragFiles);
                }
            }   

            /*---------- SOUND MANAGEMENT ----------*/

            private function loadSound():void
            {
                SoundObj = new Sound();
                SoundObj.load(new URLRequest(PlayList.Sounds[soundIndex]["url"]));
                SoundObj.addEventListener(Event.COMPLETE, onSoundLoaded);
            }

            private function onSoundLoaded(event:Event):void
            {
                // Retrieve data for current sound from playlist.
                var soundData:Object = PlayList.Sounds[soundIndex];

                // Place ID3 information into the readout panel.
                //txtID3.htmlText = Utility.ReadoutHTML(soundData["title"], soundData["track"], soundData["album"], soundData["artist"], soundData["year"], soundData["duration"]);

                // Configure the holizontal slider to act as a playhead.
                sldrPosition.maximum = soundData["duration"];

                // Set the selected row in the playlist display.
                dgPlaylist.selectedIndex = soundIndex;

                // Start the player if the mode is correct.
                if (playMode == PlayModes.LOADTOPLAY)
                {
                    playSound();
                }
                else
                {
                    playMode = PlayModes.LOADED;                    
                }

            }

            // Plays the current sound.
            public function playSound():void
            {                   
                // Load sound into channel.
                Channel.stop();
                Channel = SoundObj.play(sldrPosition.value,0,Transform);
                playMode = PlayModes.PLAY;

                // Start position timer.
                thumbTimer.start();

                // Configure UI controls.
                btnPlay.visible = false;
                btnPause.visible = true;
                sldrPosition.enabled = true;
                btnPlay.enabled = true;
                btnStop.enabled = true;
                setBackForwardButtons();

                Channel.addEventListener(Event.SOUND_COMPLETE, onSoundComplete);
            }       

            private function setBackForwardButtons():void
            {
                if (soundIndex == PlayList.Sounds.length-1 || PlayList.Sounds.length == 0)
                {
                    btnForward.enabled = false;
                }
                else
                {
                    btnForward.enabled = true;
                }

                if (soundIndex == 0 || PlayList.Sounds.length == 0)
                {
                    btnBack.enabled = false;
                }
                else
                {
                    btnBack.enabled = true;
                }
            }

            // Stops the current sound.
            public function stopSound():void
            {
                Channel.stop();             

                thumbTimer.stop();
                sldrPosition.value = 0;
                playMode = PlayModes.STOP;

                //Visualizer.graphics.clear();

                btnPlay.visible = true;
                btnPause.visible = false;
            }

            // Pause a playing sound.               
            private function pauseSound():void
            {
                Channel.stop();             
                thumbTimer.stop();
                btnPlay.visible = true;
                btnPause.visible = false;
            }

            // Change the sound index
            private function changeSoundIndex(delta:int):void
            {
                stopSound();
                playMode = PlayModes.LOADTOPLAY;
                soundIndex = soundIndex + delta;
                loadSound();
            }


            // Change the volume and panning via the sound transform object.
            private function ChangeSoundTransform():void
            {
                volume = Math.round(sldrVolume.value);
                Channel.soundTransform = new SoundTransform(volume/100, panning);
            }

            // Handles event for sound completing.
            private function onSoundComplete(event:Event):void
            {
                stopSound();
                soundIndex++;

                if (soundIndex < PlayList.Sounds.length)
                {
                    playMode = PlayModes.LOADTOPLAY;
                    loadSound();
                }

            }

            // Load ID3 information into local variables.  
            // Update the readout panel.
            // Configure position slider.
            private function onID3(event:Event):void
            {
                var sound:Sound = Sound(event.target);
                ID3 = ID3Info(sound.id3);
                Duration = Math.floor(sound.length);

                // Load sound id3 data into the playlist.
                PlayList.AddSound(ID3.songName, ID3.album, ID3.artist, ID3.track, ID3.year, ID3.genre, Duration, sound.url);

                // Increment the loaded file count.
                loadedFileCount++;

                if (loadedFileCount == selectedFileCount * 2)
                {
                    // Refresh the playlist so that new results will be visually displayed.
                    PlayList.Sounds.refresh();

                    // Set the count properties.
                    selectedFileCount = 0;
                    loadedFileCount = 0;

                    soundIndex = 0;
                    if (playMode == PlayModes.LOADTOPLAY) loadSound();
                }

            }

            /*---------- VISUALIZATION ----------*/

            private function UpdateVisualizer():void
            {
                // Instantiate a new byte array to contain spectrum data.
                Spectrum = new ByteArray();

                // Clear the visualizer graphics.
                //Visualizer.graphics.clear();

                // Dump the spectrum data into the byte array.
                SoundMixer.computeSpectrum(Spectrum,false,0);

                var f:Number;
                var i:int;
                var ave:int;

                //Visualizer.graphics.lineStyle(1, VISUALIZER_COLOR,1);
                //Visualizer.graphics.beginFill(VISUALIZER_COLOR, 0.75);


                for (i = 0; i < 512; i=i+10) 
                {
                    f = Spectrum.readFloat();
                    //Visualizer.drawRoundRect(Math.floor(i*0.7) + 7, cvsReadout.height - 10, 4, -Math.abs(f) * (cvsReadout.height-10));
                }
                //Visualizer.graphics.endFill();

            }






            // Updates the position of the hslider thumb.
            private function onTimerTick(event:TimerEvent):void
            {
                sldrPosition.value = Math.round(Channel.position);
            }


            // Update the wave visualizer if the sound is playing.
            private function onEnterFrame(event:Event):void
            {
                if (playMode == PlayModes.PLAY)
                {
                    UpdateVisualizer();
                }
            }

            // Show application information.
            private function showHowlerInfo():void
            {
                //cvsAbout.visible = true;
            }

            private function togglePlayList():void
            {

            }

            private function onItemDoubleClick(event:Event):void
            {
                this.playMode = PlayModes.LOADTOPLAY;
                thumbTimer.stop();
                sldrPosition.value = 0;
                loadSound();
            }

            /*---------- ERROR HANDLING ----------*/
            // Handles IO errors.
            private function onIOError(event:IOErrorEvent):void
            {
                //Alert.show("File load error: " + event.text);
            }

            private function startMove():void
            {
                stage.nativeWindow.startMove();
            }

            private function unloadSound():void
            {
                stopSound();
                txtID3.text = "";
                btnPlay.visible = true;
                btnPause.visible = false;
                btnPlay.enabled = false;
                btnStop.enabled = false;
            }

            private function removeSound():void
            {
                var index:int = dgPlaylist.selectedIndex;

                if (index >= 0)
                {
                    if (index == soundIndex)
                    {
                        unloadSound();
                    }
                    PlayList.RemoveSoundAt(index);
                    PlayList.Sounds.refresh();
                    setBackForwardButtons();
                }
            }

            private function removeAllSounds():void
            {
                unloadSound();
                PlayList.Sounds.removeAll();
                PlayList.Sounds.refresh();
                setBackForwardButtons();
            }

            private function onKeyDown(event:KeyboardEvent):void
            {
                if (event.charCode.toString() == "8" || event.charCode.toString() == "127")
                {
                    removeSound();          
                }
            }
        ]]>
    </fx:Script>
</s:View>

I'm new to Flex so I don't know if this is causing the problem or not, but the Howler app uses an MX:DataGrid defined like this:

<mx:DataGrid x="6" 
                     y="7" 
                     width="388"
                     height="310"
                     id="dgPlaylist"
                     keyDown="onKeyDown(event)"
                     dragMoveEnabled="true"
                     doubleClickEnabled="true"
                     dragEnabled="true"
                     dropEnabled="true"
                     dragComplete="onPlaylistDragDrop(event)"
                     itemDoubleClick="onItemDoubleClick(event)">
            <mx:columns>
                <mx:DataGridColumn width="168" headerText="Title" dataField="title" />
                <mx:DataGridColumn width="160" headerText="Album" dataField="album"/>
                <mx:DataGridColumn width="60" headerText="Duration" dataField="duration" textAlign="right" itemRenderer="components.DurationFormatter"/>
            </mx:columns>
        </mx:DataGrid>

It has additional columns that I'm not using. Could this be the cause?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤芳又自赏 2024-11-16 04:11:10

我们将需要错误消息。一般来说,您遇到的情况称为空指针异常(就像错误所述)。这基本上意味着您尝试访问空对象的属性。例如,运行以下代码将导致相同的问题

var arrayCollection:ArrayCollection;
arrayCollection.addItem( new Object() );

要修复错误,您应该在代码中找到错误引用的对象,并确保当程序执行它所抱怨的任何行时它不为空,或者在它为空的情况下添加一个 if 循环。

We are going to need the error message. Generally what you are experiencing is called a null pointer exception ( just like the error said ). Which basically means you tried to access a property of an object that is null. For example, running the following code will cause the same problem

var arrayCollection:ArrayCollection;
arrayCollection.addItem( new Object() );

To fix your error, you should find the object that the error is referring to in your code and make sure it isn't null when your program executes whatever line it is complaining about, or add an if-loop in the event it is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文