mjhouseman
mjhouseman

Reputation: 163

AsyncTask not receiving calling activity context

I'm getting the error message "android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application.

Yes, I know this error is all over stackoverflow and it's based on not getting the context of the activity... however, I am pulling that context and still receiving the error.

From MainActivity:

new MemberStream(this).execute();

From MemberStream: >

public HomeActivity activity;
ProgressDialog dialog;

public MemberStream(HomeActivity a) {
    this.activity = a;
    dialog = new ProgressDialog(a.getApplicationContext());
}
@Override
protected void onPreExecute() {
    this.dialog.setMessage("Loading");
    this.dialog.show();
}
@Override
protected Boolean doInBackground(String... params) {
    updateMembers(url, 0);
    return true;
}

When I run the application, I get the preceding error on

dialog = new ProgressDialog(a.getApplicationContext());

Any ideas?

Upvotes: 0

Views: 86

Answers (3)

Raghunandan
Raghunandan

Reputation: 133560

 dialog = new ProgressDialog(a); 

You already have a activity context passed to the asynctask constructor.

To know when to use activity context and when to use application context check the link below and answer by commonsware

When to call activity context OR application context?

Upvotes: 1

stinepike
stinepike

Reputation: 54692

You can use this

public Context context ;
ProgressDialog dialog;

public MemberStream(Context c) {
    context = c;
    dialog = new ProgressDialog(c);
}

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157457

public Activity activity;
ProgressDialog dialog;

public MemberStream(Activity a) {
    this.activity = a;
    dialog = new ProgressDialog(a);
}

You alread have a context object (Your activity)

Upvotes: 3

Related Questions