ProgressDialog没有出现在AsyncTask中

时间:2012-07-02 19:00:00

标签: android android-asynctask progressdialog

我在从Web服务检索数据时使用AsyncTask。我想从Web服务器获取数据时出现ProgressDialog。以下是代码段。

我初始化进度对话框并在onCreate中调用异步任务对象并调用asynctask:

public class MyDashboardActivity extends Activity {
     ProgressDialog mProgressDialog;
     static final int LOADING_DIALOG = 0;

private FetchDashboardEntriesProcess mTask;
private boolean mShownDialog;
    private MyDashboardActivity act = null;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.mydashboard);
mTask = new FetchDashboardEntriesProcess(MyDashboardActivity.this);
     mTask.execute(null,null,null);  
}

private class FetchDashboardEntriesProcess extends AsyncTask<Object, Void, Void> {

private MyDashboardActivity activity;
private boolean completed;



private ArrayList<DashboardEntry> returnVal = new ArrayList<DashboardEntry>();

private FetchDashboardEntriesProcess(MyDashboardActivity activity) {
    this.activity = activity;
}

public ArrayList<DashboardEntry> getAllDashboardEntry() {
    return returnVal;
}

@Override
protected void onPreExecute() {
    if (!completed) {

        activity.showDialog(LOADING_DIALOG);

    }
}

@Override
protected Void doInBackground(Object... params) {
    try {
        ServiceCall call = new ServiceCall();
        DashboardEntryCriteria bean = new DashboardEntryCriteria();
        StringBuilder dateStr = new StringBuilder();

        bean.setLoginId("300");
        returnVal = call.getDashboardEntries(bean);

    } catch (Exception e) {
        Log.d(TAG, "Exception" + e.toString());
        e.printStackTrace();
    }
    return null;

}

@Override
protected void onPostExecute(Void unused) {
    completed = true;

    notifyActivityTaskCompleted();

}

private void setActivity(MyDashboardActivity activity) {
    this.activity = activity;
    if (completed) {
        notifyActivityTaskCompleted();
    }
}
private void notifyActivityTaskCompleted() {
    if (null != activity) {
        activity.onTaskCompleted();

    }
}
}
private void onTaskCompleted() {
    Log.e(TAG, "Activity " + this
            + " has been notified the task is complete.");
    dashboardEntries = mTask.getAllDashboardEntry();
    fillDashboardEntries(dashboardEntries);

    if (mShownDialog) {
        dismissDialog(LOADING_DIALOG);

    }
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
    super.onPrepareDialog(id, dialog);
    if (id == LOADING_DIALOG) {
        mShownDialog = true;
    }
}

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == LOADING_DIALOG) {
        mProgressDialog = Functions.getProgressDialog(act,
                getString(R.string.all_retriving_data));
        return mProgressDialog;
    }
    return super.onCreateDialog(id);
}
}

根据日志程序在行onPreExecute的{​​{1}}处中断,所以我猜测由于我创建对话框的方式出错而生成错误。我如何通过这个?

谢谢。

编辑:Log Cat

activity.showDialog(LOADING_DIALOG);

3 个答案:

答案 0 :(得分:1)

在onPreExecute中,尝试ProgressDialog.show

答案 1 :(得分:1)

不是将Activity传递给AsyncTask,只需在名为showDialog()的Activity中创建一个方法,然后从onPreExecute()和{{1}调用此方法}}。在那里创建并显示您的onPostExecute()。因为您创建了ProgressDialog作为内部类,所以它应该能够在AsyncTask中调用该方法没有问题,然后您不必担心传入Activity

Context开发者页面示例类似:http://developer.android.com/reference/android/os/AsyncTask.html

答案 2 :(得分:1)

对我而言,变量act似乎总是null

将活动分配到act或尝试此操作:

mProgressDialog = Functions.getProgressDialog(MyDashboardActivity.this,
                getString(R.string.all_retriving_data));