我有一个按钮,开始下载显示进度dailog。进度dailog有一个取消按钮,可以停止下载。但是当点击取消时,下载停止,但整个应用程序崩溃而不保持主要活动 Q)如何阻止这种情况发生并保持主动 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 :(得分:0)
删除此行。
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);
}
}
当你的取消方法被调用时,你正在完成你的活动。它试图处理不存在的视图。所以它应该是这样的。
{{1}}
答案 1 :(得分: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类中