我尝试在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);
}
});
答案 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/ 有关上下文的更详细的理解