进度对话框未显示在Async任务android中

时间:2015-12-11 11:11:08

标签: android facebook progressdialog

我正在使用Facebook SDK将照片上传到Facebook。当照片上传时,我想显示一个进度对话框,我面临一个奇怪的问题。

当我在ProgressDialog方法中声明onPreExecute()时,会显示对话框。

protected void onPreExecute() {
    super.onPreExecute();
    ProgressDialog dialog = new ProgressDialog(Second.this);
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setCancelable(true);
    dialog.setMessage("Uploading...");
    dialog.show();
}

enter image description here

但是当我在全球宣布它时,没有任何事情发生。

class newPhotoUpload extends AsyncTask<Void, Void, Void> {
    ProgressDialog dialog = new ProgressDialog(Second.this);
    Intent viewIntent;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(true);
        dialog.setMessage("Uploading...");
        dialog.show();
    }

enter image description here

为什么会这样?

1 个答案:

答案 0 :(得分:2)

将进度条设为全局变量(MainActivity extends Activity下方),例如

ProgressDialog dialog=null;

然后

class newPhotoUpload extends AsyncTask<Void, Void, Void> {

    Intent viewIntent;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(Second.this);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dialog.setCancelable(true);
        dialog.setMessage("Uploading...");
        dialog.show();
    }