0

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?

4

3 回答 3

3
public Activity activity;
ProgressDialog dialog;

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

You alread have a context object (Your activity)

于 2013-05-24T14:40:03.097 回答
1
 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?

于 2013-05-24T14:43:33.550 回答
0

You can use this

public Context context ;
ProgressDialog dialog;

public MemberStream(Context c) {
    context = c;
    dialog = new ProgressDialog(c);
}
于 2013-05-24T14:41:53.163 回答