我有一个内部类,可以从服务器下载一些图像。问题是ProgressDialog没有解除()onPostExecute()方法,也不明白为什么。 据我所知,进度对话框应该显示在PreExecute()方法上,并且在doInBackground()代码完成后,在onPostExecute()方法中对话框应该被忽略。你知道我在这里做错了什么吗?谢谢。
/**
* Download images from server
*/
public class DownloadAsyncTask extends AsyncTask<Void, Integer, Void> {
private ProgressDialog mDialog;
// execution of result of Long time consuming operation
protected void onPostExecute(Void result) {
// progressDialog.show();
if (mDialog.isShowing()) {
mDialog.dismiss();
}
}
// Things to be done before execution of long running operation.
protected void onPreExecute() {
mDialog = ProgressDialog
.show(ImagesActivity.this, getString(R.string.pleasewait),
getString(R.string.loading));
}
// perform long running operation operation
protected Void doInBackground(Void... params) {
System.out.println("doInBackground loading.." + id);
String tempPath = FileUtils.createTempFile(id);
for (int i = 0; i < imagePaths.size(); i++) {
imagePaths.get(i).trim();
try {
Bitmap imgTemp;
imgTemp = FileUtils.downloadBitmapFromURL(id,
imagePaths.get(i), tempPath);
System.out.println("imgTemp: " + imgTemp);
if (imgTemp != null) {
// save image on sdcard.
// compress it for performance
Bitmap img = Bitmap.createScaledBitmap(imgTemp, 90, 80,
true);
imgTemp.recycle();
FileUtils.saveDataToFile(img, tempPath,
imagePaths.get(i));
} else {
continue;
}
} catch (IOException e) {
e.printStackTrace();
mDialog.dismiss();
}
}
Looper.prepare();
mDialog.dismiss();
return null;
}
/*
* Things to be done while execution of long running operation is in
* progress.
*/
protected void onProgressUpdate(Integer... values) {
if (mDialog.isShowing()) {
mDialog.dismiss();
}
}
}
答案 0 :(得分:1)
实际上你要做的是从另一个线程访问UI Thread
并且这是不可能的,在你使用AsyncTask
类的情况下,你可以正确,轻松地使用UI线程必须操纵线程和/或处理程序。使用onPostExecute(Result)
访问UI线程。
所以这应该有用
protected void onPostExecute(Void result) {
progressDialog.show();
if (mDialog.isShowing()) {
mDialog.dismiss();
}
}
答案 1 :(得分:1)
我一直在努力解决同样的问题。以下是我如何解决它,看看文档的这一部分:
始终创建对话框并将其显示为活动的一部分。您 通常应该在您的Activity中创建对话框 onCreateDialog(int)回调方法。当你使用这个回调时, Android系统自动管理每个对话框的状态 将它们挂钩到Activity,有效地使它成为每个的“拥有者” 对话框
注意:如果您决定在onCreateDialog()之外创建一个对话框 方法,它不会附加到Activity。但是,你可以 使用setOwnerActivity(Activity)将其附加到Activity。
来自: http://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog
这是您必须在您的活动
上设置的示例 @Override
protected void onPrepareDialog(int id, Dialog dialog)
{
//This doesn't do anything
if (id == DIALOG_PROGRESS_ID) {
((ProgressDialog)dialog).setIndeterminate(true);
}
super.onPrepareDialog(id, dialog);
}
@Override
protected Dialog onCreateDialog(int id)
{
if (id == DIALOG_PROGRESS_ID) {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage("Loading");
dialog.setCancelable(false);
dialog.setIndeterminate(true);
return dialog;
}
return null;
}
然后你可以打电话
只要您有对活动实例的引用,就可以从任何位置myActivity.showDialog(myActivity.DIALOG_PROGRESS_ID)
,myActivity.dismissDialog(myActivity.DIALOG_PROGRESS_ID)
。
答案 2 :(得分:0)
使用处理程序和onPostExecute()发送处理程序消息以关闭进度对话框。 您可以通过此链接ProgressDialog dismissal in android
获得帮助答案 3 :(得分:0)
您的代码运行正常,但是您可以检查控件是否已在我尝试过的onPostExecute()
方法中达到
package com.alarm.activity;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
public class AlarmManagerActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set up main content view
setContentView(R.layout.main);
new DownloadAsyncTask().execute();
}
/**
* Download images from server
*/
public class DownloadAsyncTask extends AsyncTask<Void, Integer, Void> {
private ProgressDialog mDialog;
// execution of result of Long time consuming operation
@Override
protected void onPostExecute(Void result) {
// progressDialog.show();
if (mDialog.isShowing()) {
mDialog.dismiss();
}
}
// Things to be done before execution of long running operation.
@Override
protected void onPreExecute() {
mDialog = ProgressDialog.show(AlarmManagerActivity.this, "Hello", "Test");
}
// perform long running operation operation
@Override
protected Void doInBackground(Void... params) {
//System.out.println("doInBackground loading.." + id);
/* String tempPath = FileUtils.createTempFile(id);
for (int i = 0; i < imagePaths.size(); i++) {
imagePaths.get(i).trim();
try {
Bitmap imgTemp;
imgTemp = FileUtils.downloadBitmapFromURL(id, imagePaths.get(i), tempPath);
System.out.println("imgTemp: " + imgTemp);
if (imgTemp != null) {
// save image on sdcard.
// compress it for performance
Bitmap img = Bitmap.createScaledBitmap(imgTemp, 90, 80, true);
imgTemp.recycle();
FileUtils.saveDataToFile(img, tempPath, imagePaths.get(i));
}
else {
continue;
}
}
catch (IOException e) {
e.printStackTrace();
mDialog.dismiss();
}
}
Looper.prepare();
mDialog.dismiss();*/
try {
Thread.sleep(5000);
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/*
* Things to be done while execution of long running operation is in
* progress.
*/
@Override
protected void onProgressUpdate(Integer... values) {
if (mDialog.isShowing()) {
mDialog.dismiss();
}
}
}
}
我认为doInbackground()
方法存在问题。我只是运行线程睡眠5秒,然后控制到达post()
方法并关闭进度对话框。