Android android.view.windowmanager $ badtokenexception progressdialog

时间:2014-04-05 19:29:37

标签: android

我尝试在Button点击中使用progressDialog,但我有android.view.windowmanager $ badtokenexception错误。这是我的代码:

starus_fail.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            dialog = ProgressDialog.show(getApplicationContext(),
                    "Please Wait... ", "Loading... ");
            dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);

            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {

                    somefunction();
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }

            }, 1000);

        }
    });

2 个答案:

答案 0 :(得分:1)

不要将应用程序上下文用于创建对话框。而是使用Activity上下文。在这里,您可以从View.getContext()获取它。所以将你的PorgressDialog创建替换为:

dialog = ProgressDialog.show(arg0.getContext(), "Please Wait... ", "Loading... ");

答案 1 :(得分:0)

为了得到上述答案,ProgressDialog使用调用它的Activity的上下文。

所以只需传递

 YourActivity.this

而不是

getApplicationContext

见www.doubleencore.in/2013/06/context/ 有关上下文的更详细的理解