2013-03-10 5 views
0

Я хочу загрузить изображение с URL в мой Mobile с помощью Flex Mobile. Я нашел This и попытался это:Невозможно загрузить изображение из Интернета в мобильном мобильном приложении

<?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="HomeView"> 


<fx:Declarations> 
    <!-- Place non-visual elements (e.g., services, value objects) here --> 
    <fx:Model id="fileRefModel"> 
     <file> 
      <creationDate>{""}</creationDate> 
      <creator>{""}</creator> 
      <modificationDate>{""}</modificationDate> 
      <name>{""}</name> 
      <size>{""}</size> 
      <type>{""}</type> 
     </file> 
    </fx:Model> 
</fx:Declarations> 
<fx:Script> 
    <![CDATA[   
     import mx.collections.ArrayCollection; 
     import flash.net.FileReference; 

     [Bindable] 
     private var arrColl:ArrayCollection; 

     // URL of the file to download. 
     private const FILE_URL:String = "http://www.thebiblescholar.com/android_awesome.jpg"; 

     private var fileRef:FileReference; 
     private var urlReq:URLRequest; 

     private function init():void { 
      // Initialize the array collection to an empty collection. 
      arrColl = new ArrayCollection(); 

      // Set up the URL request to download the file specified by the FILE_URL variable 
      urlReq = new URLRequest(FILE_URL); 

      // Define file reference object and add a bunch of event listeners. 
      fileRef = new FileReference(); 
      fileRef.addEventListener(Event.CANCEL, doEvent); 
      fileRef.addEventListener(Event.COMPLETE, doEvent); 
      fileRef.addEventListener(Event.OPEN, doEvent); 
      fileRef.addEventListener(Event.SELECT, doEvent); 
      fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent); 
      fileRef.addEventListener(IOErrorEvent.IO_ERROR, doEvent); 
      fileRef.addEventListener(ProgressEvent.PROGRESS, doEvent); 
      fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent); 
     } 

     private function doEvent(evt:Event):void { 
      // Create shortcut to the FileReference object. 
      var fr:FileReference = evt.currentTarget as FileReference; 

      // Add event order and type to the DataGrid control. 
      arrColl.addItem({data:arrColl.length+1, type:evt.type, eventString:evt.toString()}); 

      try { 
       // Update the Model. 
       fileRefModel.creationDate = fr.creationDate; 
       fileRefModel.creator = fr.creator; 
       fileRefModel.modificationDate = fr.modificationDate; 
       fileRefModel.name = fr.name; 
       fileRefModel.size = fr.size; 
       fileRefModel.type = fr.type; 
       // Display the Text control. 
       //txt.visible = true; 
      } catch (err:*) { 
       // uh oh, an error of sorts. 
      } 
     } 

     private function downloadSourceCodeZip():void { 
      // Clear existing array collection. 
      arrColl = new ArrayCollection(); 

      fileRef.download(urlReq); // Here it gives me Exception >> (Suspended: TypeError: Error #1009: Cannot access a property or method of a null object reference.) 

     }  


    ]]> 
</fx:Script>  

<s:Button label="Download" click="downloadSourceCodeZip()"/> 

, но я получаю Suspended: TypeError: Error #1009: Cannot access a property or method of a null object reference., как сделать этот код работать для меня>

+0

Запустите его в отладчике, чтобы узнать (наведите указатель мыши на свой код), что именно равно null. –

ответ

0

Это не выглядит, как ваш Init() метод вызывался из в любом месте, поэтому fileRef не создается.

Вам необходимо добавить initialize="init()" в ваш тег вида.