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.ViewRootImpl.setView(ViewRootImpl.java:691)
 at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
 at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
 at android.widget.Toast$TN.handleShow(Toast.java:434)
 at android.widget.Toast$TN$2.handleMessage(Toast.java:345)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6119)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

从报错信息中,我得出这样一个结论:

在Android6.0以前,使用windowManager添加一个悬浮窗时,我使用的悬浮窗类型是TYPE_TOAST,即
params.type = WindowManager.LayoutParams.TYPE_TOAST;
windowManager.addView(contentView, params);
但是系统升级Android7.0后,就会报ndroid.view.WindowManager$BadTokenException: Unable to add window -- window [email protected] has already been added
的错,具体意思是Toast已经被添加到窗口中,所以这时候我们可以把TYPE_TOAST换成TYPE_PHONE,这样就可以显示悬浮窗了,程序也不会crash。

这里附上添加悬浮窗的代码,仅供参考:

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new
WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_PHONE;
params.format = PixelFormat.RGBA_8888;
params.gravity = Gravity.LEFT | Gravity.TOP;
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
params.width = windowManager.getDefaultDisplay().getWidth();
params.height = windowManager.getDefaultDisplay().getHeight();
params.x = 0;
params.y = 0;
windowManager.addView(contentView, params);      

//这里要的contentview需要自己定义,而且添加悬浮窗需要动态申请悬浮窗权限。
时间: 2025-01-01 23:44:24

Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added的相关文章

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

关于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",tr

cordova platform add android报错问题处理

第一次在博客园写博客,有错误的还请大神不要喷,互相学习么,我也是正在学习中,这是我遇到的问题及解决的办法. cordova platform add android 报错内容大致是匹配不到cordova-android版本,在百度上查的资料说是android sdk环境没配置成功,于是我执行android -h 发现报错:"找不到路径". 于是在晚上又百度,说是在环境变量path里面添加%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\Sy

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文件中

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

WindowManager$BadTokenException: Unable to add window permission denied for this window type

10-11 11:47:27.472: E/AndroidRuntime(12804): java.lang.RuntimeException: Unable to start activity ComponentInfo{cn.zhl.classguiding/cn.zhl.classguiding.XueanPdfActivity}: android.view.WindowManager$BadTokenException: Unable to add window [email prote

cocos2dx在windows下搭建环境android报错

报错:Program bash is not found in PATH (如果按照我的方法来的话是没有这个错误的,我之前用别的方法的时候有但是后来还是没解决,写出来放到这里做参考吧) 参考原文:http://blog.csdn.net/fuyongbing1986/article/details/11556149 方法: 1.把cygwin/bin加入系统环境变量Path下 2.右击工程-->properties-->C/C++ Build -->ToolChain Edit    确

CentOS中输入yum报错:sudo: unable to execute /bin/yum: No such file or directory

今天尝试更新了下虚拟机CentOS中的python版本后. 运行“yum”命令,就报错:“sudo: unable to execute /bin/yum: No such file or directory” 查询了下网上的资料发现,原来yum调用是用python写的.遂想起刚刚更新python版本,忘了修改yum配置文件了. 解决办法:修改yum配置文件  [[email protected] ~]#vim /usr/bin/yum 把文件头部的#!/usr/bin/python改成#!/u