2011-06-27 75 views
1

我有一個TextArea和一個DataGrid。我已經使用目錄中的文本文件列表填充了DataGrid。現在我試圖將選定的文件加載到TextArea中。從列表中選擇一個文件時,我得到一個空指針異常,這告訴我我沒有正確訪問它......我想。Flex - 嘗試從列表中加載文本文件到TextArea

<s:TextArea id="mainTextField" x="0" y="0" width="730" height="523" 
        editable="true" enabled="true"/> 
     <s:DataGrid id="list" x="730" y="0" width="294" height="523" dataProvider="{files}" gridClick="listHandler(event)"> 
      <s:columns> 
       <s:ArrayList> 
        <s:GridColumn dataField="name" headerText="Filename"/>  
       </s:ArrayList> 
       </s:columns> 

     </s:DataGrid> 

... 

    private var files:ArrayCollection; 

       public var stream:FileStream = new FileStream(); // The FileStream object used for reading and writing the currentFile 
       public var currentFile:File; 

    private function init():void 
       { 

        // Default on start: resolve desktop directory 
        var desktop:File = File.desktopDirectory; 
        // Get desktop directory listing 
        files = new ArrayCollection(desktop.getDirectoryListing()); 
       } 

       protected function listHandler(event:GridEvent):void 
       { 
        currentFile = event.target as File; 
        trace(currentFile.nativePath.toString()); **//Error occurrs here** 
        stream = new FileStream(); 
        stream.openAsync(currentFile, FileMode.READ); 
        stream.addEventListener(Event.COMPLETE, fileReadHandler); 
        stream.addEventListener(IOErrorEvent.IO_ERROR, readIOErrorHandler); 
       } 

       public function fileReadHandler(event:Event):void 
       { 
        var str:String = stream.readUTFBytes(stream.bytesAvailable); 
        stream.close(); 
        var lineEndPattern:RegExp = new RegExp(File.lineEnding, "g"); 
        str = str.replace(lineEndPattern, "\n"); 
        mainTextField.text = str; 
        stream.close(); 
       } 

回答

2

我不是這個100%肯定,但我不認爲event.target是在GridClick事件的行(即文件)。我認爲GridClick事件更重要的是選擇了一個單元格。嘗試

currentFile = list.selectedItem as File; 

如果不行,試試這個:

curentFile = files.getItemAt(event.rowIndex) as File; 
+0

這是第二個:)謝謝你這麼多! – RapsFan1981

相關問題