2015-07-03 64 views
0

我創建正在下載一個文件,然後加載它到一個ImageView的一個應用程序,但我收到此錯誤位圖的錯誤與ImageView的

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference 

,這是我的下載代碼

class DownloadFileFromURL extends AsyncTask<String, String, String> { 


     // Show Progress bar before downloading Music 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // Shows Progress Bar Dialog and then call doInBackground method 
      showDialog(progress_bar_type); 
     } 

     // Download File from Internet 
     @Override 
     protected String doInBackground(String... f_url) { 
      int count; 
      try { 
       URL url = new URL(f_url[0]); 
       URLConnection conection = url.openConnection(); 
       conection.connect(); 
       // Get Music file length 
       int lenghtOfFile = conection.getContentLength(); 
       // input stream to read file - with 8k buffer 
       InputStream input = new BufferedInputStream(url.openStream(),10*1024); 
       // Output stream to write file in SD card 
       int imageNr = sharedPreferences.getInt("ImageNr", 1); 
       OutputStream output = new FileOutputStream(getApplicationInfo().dataDir+"/files/"+imageNr+".jpg"); 

       byte data[] = new byte[1024]; 
       long total = 0; 
       while ((count = input.read(data)) != -1) { 
        output.write(data, 0, count); 
        total += count; 
        // Publish the progress which triggers onProgressUpdate method 
        publishProgress("" + (total)); 

        // Write data to file 

       } 
       // Flush output 
       output.flush(); 
       // Close streams 
       output.close(); 
       input.close(); 
      } catch (Exception e) { 
       Log.e("Error: ", e.getMessage()); 
      } 
      return null; 
     } 
     // While Downloading Music File 
     protected void onProgressUpdate(String... progress) { 
      // Set progress percentage 
      prgDialog.setProgress(Integer.parseInt(progress[0])); 
     } 

     // Once File is downloaded 
     @Override 
     protected void onPostExecute(String file_url) { 
      // Dismiss the dialog after the Music file was downloaded 
      dismissDialog(progress_bar_type); 

      int imageNr = sharedPreferences.getInt("ImageNr", 1); 
      File imgFile = new File(getApplicationInfo().dataDir+"/files/"+imageNr+".jpg"); 
      imageNr++; 
      SharedPreferences.Editor editorsave = sharedPreferences.edit(); 
      editorsave.putInt("ImageNr", imageNr); 
      editorsave.apply(); 

      if(imgFile.exists()){ 
       Toast.makeText(getApplicationContext(), "Bild laddad!", Toast.LENGTH_LONG).show(); 

       Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

       imageView.setImageBitmap(myBitmap); 

      }else{ 
       Toast.makeText(getApplicationContext(), "Bild ej hittad!", Toast.LENGTH_LONG).show(); 
      } 
      // Do stuff here 

     } 
    } 

耶它使用了一些不推薦的代碼,但這在我的其他應用程序中適用於我,儘管我沒有在那個中使用位圖,但AsyncTask起作用。

+0

檢查實例,也爲ImageView的我沒有看到聲明..位圖的 – Proxytype

+0

實例? 同樣如下所述,imageView是在MainActivity之後聲明的(未在上面的代碼中顯示) –

+0

@Emillio在運行應用程序時是否使用設備的任何物理電纜? – Ranjit

回答

1

imageView是空

  • 從異常本身留方式

    if(imgFile.exists() && imageView != null) { 
    .... 
    
  • 檢查imageView參考分配。這似乎imageView引用不正確分配,因此返回位圖的空

+0

imageView是在MainActivity類之後聲明的,就像 'ImageView imageView;' –

+0

您能詳細說明一下嗎?分配一個值?我不記得給imageView分配一個值?它在代碼中給出了圖像,對不起,我不明白 –

+0

@EmilioGaines您需要分配一個有效的對象以供參考。通常在你的活動'onCreate() - setContentView()'後面'只需添加'imageView =(ImageView)findViewIdById(R.id.YOUR_IMAGE_VIEW_ID);'。 –