Reputation: 487
I've using AsyncTask
while retrieving data from a web service. I want to a ProgressDialog
to appear while I fetch the data from web server. Following is the code snippet.
I initialize progress dialog and call the async task object inside onCreate and call 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);
}
}
According to the log program is interrupted at onPreExecute
at the line activity.showDialog(LOADING_DIALOG);
so I'm guessing the error is generated due to something being wrong with the way I create dialog. How do I get past this?
Thank you.
EDIT: Log Cat
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)
Upvotes: 1
Views: 1114
Reputation: 6598
For me it looks like variable act
is always null
.
Assing activity to act
or try this:
mProgressDialog = Functions.getProgressDialog(MyDashboardActivity.this,
getString(R.string.all_retriving_data));
Upvotes: 1
Reputation: 10540
Rather than passing an Activity
in to the AsyncTask
, just create a method inside of the Activity called showDialog()
and then call this from the onPreExecute()
and onPostExecute()
. Create and show your ProgressDialog
in there. Because you created the AsyncTask
as an inner class it should be able to call the method in the Activity
with no problem, and then you don't have to worry about passing in Context
.
Similar to the AsyncTask
developers page example: http://developer.android.com/reference/android/os/AsyncTask.html
Upvotes: 1