博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android开发踩坑之PopupWindow "Unable to add window -- token null is not valid; is your activity running"
阅读量:4030 次
发布时间:2019-05-24

本文共 846 字,大约阅读时间需要 2 分钟。

问题现象

关键崩溃的BS如下:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?    at android.view.ViewRootImpl.setView(ViewRootImpl.java:579)    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)    at android.widget.PopupWindow.invokePopup(PopupWindow.java:1315)    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1075)    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1038)

根本原因

PopupWindow关联的Activity被销毁后,还调用showAtLocation方法

解决方案

调用showAtLocation方法时,除了调用View的post(Runnable action)(在Runable显示PopupWindow),在调用showAtLocation前需要判断一下关联的Activity是否已经被销毁

//示例代码如下if (!Activity实例.isFinishing()) {    PopupWindow实例.showAtLocation(view实现, Gravity.NO_GRAVITY, 0, 0);}

参考资料

转载地址:http://bxmbi.baihongyu.com/

你可能感兴趣的文章
android进程保活
查看>>
android源码(1)LiveData源码
查看>>
策略模式
查看>>
低灵敏度SwipeRefreshLayout
查看>>
MySQL实现主从复制
查看>>
MySQL主从复制Slave_IO_Runing和Slave_SQL_Running问题
查看>>
ubuntu14.04 环境下安装配置nginx+php5-fpm
查看>>
Memcache的使用和协议分析详解
查看>>
Laravel 学习笔记 —— 神奇的服务容器
查看>>
条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
查看>>
dpi 、 dip 、分辨率、屏幕尺寸、px、density 关系以及换算
查看>>
laravel中的自定义函数的放置规范
查看>>
laravel中创建帮助函数文件
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>
在CentOS 7系统上搭建LNMP 环境
查看>>
Centos 7(Linux)环境下安装PHP(编译添加)相应动态扩展模块so(以openssl.so为例)
查看>>
fastcgi_param 详解
查看>>
Nginx配置文件(nginx.conf)配置详解
查看>>