Unable to add window -- token null is not for an application

代码中出现如下错误:

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

分析:

问题在于new AlertDialog.Builder(Context),虽然这里的参数是AlertDialog.Builder(Context context)

但我们不能使用getApplicationContext()获得的Context,而必须使用Activity的Context对象,因为只有一个Activity才能添加一个窗体。

解决方法:

将new AlertDialog.Builder(Context context)中的参数用Activity的Context对象即可

弹出确认框

    new AlertDialog.Builder(MainActivity.this)
		.setTitle(R.string.confirm_title)
		.setMessage(R.string.confirm_content)
		.setPositiveButton(R.string.confirm_ok, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int arg1) {
				dialogInterface.dismiss();
				masterClear(getApplicationContext());
			}
		})
		.setNegativeButton(R.string.confirm_cancel, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialogInterface, int arg1) {
				dialogInterface.cancel();
			}
		}).show();
时间: 2024-08-03 04:54:04

Unable to add window -- token null is not for an application的相关文章

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

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自用-----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.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) .

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

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

Unable to add window -- token [email protected] is not valid错误分析记录

打开APP时,出现闪退的情况,查看android studio报错信息,主要为: Unable to add window -- token [email protected] is not valid 原因分析:由于进入APP时会显示一个进度对话框,对话框的初始化必须依赖Activity,但如果对话框的初始化放在Activity的onCreate方法中,那么就会报错. 因为根据Activity的生命周期,onCreate方法执行时,Activity并未创建完毕,对话框所依赖的Activity还

Android Unable to add window -- token [email protected] is not valid错误分析记录

打开APP时,出现闪退的情况,查看android studio报错信息,主要为: Unable to add window -- token [email protected] is not valid 原因分析:由于进入APP时会显示一个进度对话框,对话框的初始化必须依赖Activity,但如果对话框的初始化放在Activity的onCreate方法中,那么就会报错. 因为根据Activity的生命周期,onCreate方法执行时,Activity并未创建完毕,对话框所依赖的Activity还

Context 对应的Unable to add window 错误记录

近日在使用 AlertDialog时发现一个错误:Unable to add window -- token null is not for an application,从前面观察是无法添加一个新的窗口到这个context,后来想了想前面是用的getApplicationContext()传递过来的context, 看来不能往context中加window吧,就改成了activity.this,代表一个具体的activity了,这样就OK了,记录下以后应该能解决这种添加新窗口传递context