如何在自定义适配器中初始化ProgressDialog

时间:2016-01-17 10:02:36

标签: android

我正在尝试在自定义适配器中使用AsuncTask。当我试图使用

class UpdateProductVariantTask extends AsyncTask<String, String, Void> {

    private ProgressDialog progressDialog = new ProgressDialog(context);
    String result = "";

    protected void onPreExecute() {
        progressDialog.setCancelable(false);
        progressDialog.setMessage("Please wait.....");
        progressDialog.show();
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
                UpdateProductVariantTask.this.cancel(true);
            }
        });
    }
}

我收到此错误:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

行progressDialog.show();

如何解决此错误?

1 个答案:

答案 0 :(得分:2)

尝试使用Activity而不是上下文。

private ProgressDialog progressDialog = new ProgressDialog(activity);