1

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

我初始化进度对话框并在 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在该行被中断,activity.showDialog(LOADING_DIALOG);所以我猜测错误是由于我创建对话框的方式出现问题而产生的。我该如何度过这个难关?

谢谢你。

编辑:原木猫

07-02 19:41:53.433: D/dalvikvm(335): GC_EXTERNAL_ALLOC freed 61K, 52% free 2600K/5379K,   external 1625K/2137K, paused 79ms

 07-02 19:41:58.214: I/myproject(335): Activity com.myproject.activities.LoginActivity@405293b0 has been notified the task is complete.
 07-02 19:41:58.745: D/dalvikvm(335): GC_EXTERNAL_ALLOC freed 271K, 52% free 2721K/5639K, external 3579K/3826K, paused 152ms

 07-02 19:41:59.644: E/RESULT:(335): GetDashboardEntriesResponse{GetDashboardEntriesResult=anyType{}; }

 07-02 19:41:59.654: E/Some Exception(335): Some Exception
 07-02 19:41:59.664: W/System.err(335): java.lang.NullPointerException
 07-02 19:41:59.664: W/System.err(335):     at android.app.Dialog.<init> (Dialog.java:141)
07-02 19:41:59.664: W/System.err(335):  at android.app.AlertDialog.<init>(AlertDialog.java:63)
07-02 19:41:59.674: W/System.err(335):  at android.app.ProgressDialog.<init>(ProgressDialog.java:80)
07-02 19:41:59.674: W/System.err(335):  at android.app.ProgressDialog.<init>(ProgressDialog.java:76)
07-02 19:41:59.674: W/System.err(335):  at com.myproject.utils.Functions.getProgressDialog(Functions.java:54)
07-02 19:41:59.674: W/System.err(335):  at com.myproject.activities.MyDashboardActivity.onCreateDialog(MyDashboardActivity.java:178)
07-02 19:41:59.674: W/System.err(335):  at android.app.Activity.onCreateDialog(Activity.java:2482)
07-02 19:41:59.674: W/System.err(335):  at android.app.Activity.createDialog(Activity.java:882)
07-02 19:41:59.674: W/System.err(335):  at android.app.Activity.showDialog(Activity.java:2557)
07-02 19:41:59.674: W/System.err(335):  at android.app.Activity.showDialog(Activity.java:2524)
07-02 19:41:59.674: W/System.err(335):  at com.myproject.activities.MyDashboardActivity$FetchDashboardEntriesProcess.onPreExecute(MyDashboardActivity.java:211)
07-02 19:41:59.674: W/System.err(335):  at android.os.AsyncTask.execute(AsyncTask.java:391)
07-02 19:41:59.674: W/System.err(335):  at com.myproject.activities.MyDashboardActivity.onCreate(MyDashboardActivity.java:159)
07-02 19:41:59.674: W/System.err(335):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-02 19:41:59.684: W/System.err(335):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-02 19:41:59.684: W/System.err(335):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:1487)
07-02 19:41:59.684: W/System.err(335):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
07-02 19:41:59.684: W/System.err(335):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
07-02 19:41:59.684: W/System.err(335):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
07-02 19:41:59.684: W/System.err(335):  at android.widget.TabHost.setCurrentTab(TabHost.java:326)
07-02 19:41:59.684: W/System.err(335):  at android.widget.TabHost.addTab(TabHost.java:216)
07-02 19:41:59.684: W/System.err(335):  at com.myproject.activities.IncludeTabActivity.onCreate(IncludeTabActivity.java:52)
07-02 19:41:59.684: W/System.err(335):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-02 19:41:59.684: W/System.err(335):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-02 19:41:59.684: W/System.err(335):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-02 19:41:59.694: W/System.err(335):  at  android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-02 19:41:59.694: W/System.err(335):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-02 19:41:59.694: W/System.err(335):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 19:41:59.694: W/System.err(335):  at android.os.Looper.loop(Looper.java:123)
07-02 19:41:59.694: W/System.err(335):  at android.app.ActivityThread.main(ActivityThread.java:3683)
07-02 19:41:59.694: W/System.err(335):  at  java.lang.reflect.Method.invokeNative(Native Method)
07-02 19:41:59.704: W/System.err(335):  at java.lang.reflect.Method.invoke(Method.java:507)
07-02 19:41:59.704: W/System.err(335):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-02 19:41:59.704: W/System.err(335):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-02 19:41:59.704: W/System.err(335):  at dalvik.system.NativeStart.main(Native Method)
4

3 回答 3

1

与其将Activityin 传递给AsyncTask,只需在被调用的 Activity 中创建一个方法showDialog(),然后从onPreExecute()and调用 this onPostExecute()。在那里创建并展示你ProgressDialog的。因为您将 . 创建AsyncTask为内部类,所以应该可以Activity毫无问题地调用 . 中的方法,然后您不必担心传入Context.

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

于 2012-07-02T19:10:01.540 回答
1

在 onPreExecute 中,尝试ProgressDialog.show

于 2012-07-02T19:06:40.737 回答
1

对我来说,变量看起来act总是null

评估活动act或尝试以下操作:

mProgressDialog = Functions.getProgressDialog(MyDashboardActivity.this,
                getString(R.string.all_retriving_data));
于 2012-07-06T20:13:16.780 回答