关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window

下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application,让我百思不得其解。我这使用这种方法实现的进度对话框:proDialog = ProgressDialog.show(getApplicationContext(), "sadfs", "dddddd",true);
查证得知

导致异常的原因就是这个getApplicationContext(),虽然这里的参数也是获得一个context,但不能使用getApplicationContext()获得的Context。必须使用Activity,因为只有一个Activity才能添加一个窗体

解决方法:将参数中的getApplicationContext() 改为xxActivity.this就ok了

ProgressDialog对象是依赖于一个View的,而View是和一个Activity对应的。 于是,这里涉及到一个生命周期的问题,this.getApplicationContext()取的是这个应 用程序的Context,Activity.this取的是这个Activity的Context,这两者的生命周期是不同 的,前者的生命周期是整个应用,后者的生命周期只是它所在的Activity。而ProgressDialog应
该是属于一个Activity的,在Activity销毁的时候它也就销毁了,不会再存在;但是,如果传 入this.getApplicationContext(),就表示它的生命周期是整个应用程序,这显然超过了它 的生命周期了。 所以,在这里我们只能使用Activity的this。

来源: http://www.xuebuyuan.com/1358679.html

来自为知笔记(Wiz)

时间: 2024-08-08 03:57:29

关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window的相关文章

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

原博客地址:http://aijiawang-126-com.javaeye.com/blog/662336 在Activity中newSpinner是我把mContext传入,但是出了 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application这个错误,参考了达人的文章终于搞定. [java] view plaincopy private C

bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token

android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?at android.view.ViewRootImpl.setView(ViewRootImpl.java:546)at android.view.WindowManagerImpl.addView(WindowManagerImp

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

在广播中启动一个Dialog时出现如下错误信息:Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 在代码中添加了dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);错误信息没有消除, 此时在Manifest.xml文件中

兔子--android.view.windowmanager BadTokenException :unable to add window token null is not for applica

导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApplicationContext()获得的Context,而必须使用Activity,因为只有一个Activity才能添加一个窗体. 环境变量传入错误,不能使application的环境,应该是Activity的环境 new AlertDialog.Builder(MyActivity.this) .

android.view.WindowManager$BadTokenException: Unable to add window

解决方法:AlertDialog.Builder(xxx.this) => AlertDialog.Builder(this.getParent()) 或(this)改为(xxx.this)

bug_android.view.WindowManager$BadTokenException: Unable to add window -- token

========5 java.lang.IllegalArgumentException: View not attached to window manager at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:653) at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:349) at android.view.Wi

Android自用-----WindowManager$BadTokenException: Unable to add window -- token null is not for an application

转自http://www.cnblogs.com/oakpip/archive/2011/04/06/2007310.html 错误产生: private Context mcontext; @Override protected void onCreate(Bundle savedInstanceState) {mcontext = getApplicationContext(); System.out.println("mcontext=" + mcontext); } new A

Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added

很久之前测试通过的代码,现在手机升级了Android7.0后一运行就崩溃,报出这样的错误,具体错误如下: Process: com.example.sho.android_anti_theft, PID: 26807 android.view.WindowManager$BadTokenException: Unable to add window -- window [email protected] has already been added at android.view.ViewRoo

一、dialog.show()引起的android.view.WindowManager$BadTokenException错误

错误日志 ? 1 2 3 4 5 6 7 android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@427b7270 is not valid; is your activity running?     at android.view.ViewRootImpl.setView(ViewRootImpl.java:653)     at android.vi