2015-12-12 131 views
0

我有一個按鈕,開始下載,顯示進度dailog。進度dailog有一個取消按鈕,可以停止下載。但點擊下載站,但整個應用程序崩潰時的取消,而不保留mainactivity Q)如何阻止這種情況發生,並留在MainActivity 2)如何刪除部分下載的文件 這裏是進步dailog代碼按取消下載按鈕時,整個應用程序崩潰

DownloadFileFromURL cdrt = new DownloadFileFromURL(); 

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case progress_bar_type: // we set this to 0 
      pDialog = new ProgressDialog(this); 
      pDialog.setMessage("Downloading file. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setMax(100); 
      pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
      pDialog.setCancelable(true); 
      pDialog.setButton("Cancel", new CancelOnClickListener()); 
      pDialog.show(); 
      return pDialog; 
     default: 
      return null; 
    } 
} 
private final class CancelOnClickListener implements DialogInterface.OnClickListener { 
    public void onClick(DialogInterface dialog, int which) { 
     MainActivity.this.finish(); 
     dismissDialog(progress_bar_type); 
     if(cdrt.getStatus() == AsyncTask.Status.RUNNING){ 
     cdrt.cancel(true);} 
} 

的AsyncTask

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

    /** 
    * Before starting background thread 
    * Show Progress Bar Dialog 
    * */ 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     showDialog(progress_bar_type); 
    } 

    /** 
    * Downloading file in background thread 
    * */ 
    @Override 
    protected String doInBackground(String... f_url) { 
     int count; 
     try{ 
      URL url = new URL(f_url[0]); 

      URLConnection conection = url.openConnection(); 
      conection.connect(); 
      // this will be useful so that you can show a tipical 0-100% progress bar 
      int lenghtOfFile = conection.getContentLength(); 

      // download the file 
      InputStream input = new BufferedInputStream(url.openStream(), 8192); 

      // Output stream 
      OutputStream output = new FileOutputStream("/sdcard/download/downloaded.jpg"); 

      byte data[] = new byte[1024]; 

      long total = 0; 

      while ((count = input.read(data)) != -1) { 
       total += count; 
       // publishing the progress.... 
       // After this onProgressUpdate will be called 
       publishProgress(""+(int)((total*100)/lenghtOfFile)); 

       // writing data to file 
       output.write(data, 0, count); 
      } 

      // flushing output 
      output.flush(); 

      // closing streams 
      output.close(); 
      input.close(); 

     } catch (Exception e) { 
      Log.e("Error: ", e.getMessage()); 
     } 

     return null; 
    } 

    /** 
    * Updating progress bar 
    * */ 
    protected void onProgressUpdate(String... progress) { 
     // setting progress percentage 
     pDialog.setProgress(Integer.parseInt(progress[0])); 
    } 

    /** 
    * After completing background task 
    * Dismiss the progress dialog 
    * **/ 
    @Override 
    protected void onPostExecute(String file_url) { 
     // dismiss the dialog after the file was downloaded 
     dismissDialog(progress_bar_type); 


    } 

} 

logcat的

12-12 11:37:10.791 25155 25155 E AndroidRuntime        FATAL EXCEPTION: main 

12-12 11:37:10.791 25155 25155 E AndroidRuntime        Process: com.mycompany.myapp2, PID: 25155 

12-12 11:37:10.791 25155 25155 E AndroidRuntime        java.lang.IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog 

12-12 11:37:10.791 25155 25155 E AndroidRuntime        at android.app.Activity.missingDialog(Activity.java:3461) 

12-12 11:37:10.791 25155 25155 E AndroidRuntime        at android.app.Activity.dismissDialog(Activity.java:3446) 
12-12 11:37:10.791 25155 25155 E AndroidRuntime        at com.mycompany.myapp2.MainActivity$DownloadFileFromURL.onPostExecute(MainActivity.java:170) 
12-12 11:37:10.791 25155 25155 E AndroidRuntime        at com.mycompany.myapp2.MainActivity$DownloadFileFromURL.onPostExecute(MainActivity.java) 
12-12 11:37:10.791 25155 25155 E AndroidRuntime        at android.os.AsyncTask.finish(AsyncTask.java:636) 
12-12 11:37:10.791 25155 25155 E AndroidRuntime        at android.os.AsyncTask.access$500(AsyncTask.java:177) 
+0

裏面你傳遞正確的參數dismissDialog()? –

回答

0

刪除此行。

 MainActivity.this.finish(); 

從你的方法.. public void onClick(DialogInterface dialog, int which) {

當您正在整理當你的取消方法的活動被調用。它試圖處理不存在的視圖。所以它應該是這樣的。

private final class CancelOnClickListener implements DialogInterface.OnClickListener { 
public void onClick(DialogInterface dialog, int which) { 
    dismissDialog(progress_bar_type); 

    if (cdrt != null && cdrt.getStatus().equals(AsyncTask.Status.RUNNING)) { 
     cdrt.cancel(true); 
    } 
} 
+0

如果我刪除該行,只有dailog關閉,但下載仍在後臺進行。當我再次按下載按鈕時,出現下載進度dailog – user5524159

+0

嘗試更新此代碼。我認爲這是因爲如果(cdrt.getStatus()== AsyncTask.Status.RUNNING)條件不匹配。 – Beena

+0

但是下載仍在後臺進行 – user5524159

0

您試圖關閉您無權訪問的對話框。關閉AsyncTask類中的對話框。如果您關閉您的活動中的對話框,然後將粉碎

 pDialog = new ProgressDialog(this); 
     pDialog.setMessage("Downloading file. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setMax(100); 
     pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pDialog.setCancelable(true); 
     pDialog.setButton("Cancel", new CancelOnClickListener()); 
     pDialog.show(); 

應該是的AsyncTask類

+0

我開除了asynctask下的dailog只有 – user5524159

+0

我看到你有單獨的顯示和解除對話權的方法嗎? this showDialog(progress_bar_type); 和 dismissDialog(progress_bar_type); 只是在異步任務本身內啓動對話框而不是通過外部方法,因爲您不會在AsyncTask類以外獲得該實例 –

+0

是的我們會嘗試並讓您知道 – user5524159

相關問題