android.view.WindowManager$BadTokenException: Unable to add window
这是在加载dialog时出现的一个异常。转载地址:http://hi.baidu.com/fbdfp/item/7dea2d0ade9121813d42e23d
扔了好久的android又开始断断续续在接触。昨天不知道看了个什么东东之后就看到关于LayoutInflater的用法,于是想try一下,也就是在Button上点击一下弹出一个对话框,结果遇到一个问题,android.view.WindowManager$BadTokenException: Unable to add window 报了这个错。
private void showCustomDialog() {
// TODO Auto-generated method stub
AlertDialog.Builder builder;
AlertDialog dialog; LayoutInflater inflator = (LayoutInflater) LayoutInflatorActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.dialoglayout, null);
TextView text = (TextView) view.findViewById(R.id.textview);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);
builder = new AlertDialog.Builder(this);
builder.setView(view);
dialog = builder.create();
dialog.show(); }
在这句上报错了。因为本来我的写法是:
builder = new AlertDialog.Builder(this.getApplicationContext());
因为看了API 是 new AlertDialog.Builder(Context context);想着也没什么语法错误吧。
结果网上搜了一下,大概解释了一下获取上下文this.getApplicationContext()); 和 this的区别:
这里的this指的当然就是Acitivity.this , 指的是这个Acitivity的上下文,而this.getApplicationContext()指的则是整个应用的上下文。
对于AlertDialog来说,是需要依赖一个View,而View是对应于Activity的。
那么为什么会报错呢,这里涉及到一个生命周期的问题了。
对于一个应用Context来说,它的生命周期是整个应用程序的生命周期,而对于Activity来说,当它销毁之后它的生命周期就结束了。
AlertDialog是属于Acitivity的,当Activity销毁的时候它也必须销毁,所以这里我们指定是Activity的Context。
android.view.WindowManager$BadTokenException: Unable to add window的更多相关文章
- Activity has leaked window that was originally added -界面退出时未关闭对话框异常 android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running? -
退出Activity时弹出登录框,点击确定finish当前Activity,结果报了这个错,随后查找资料知道 原因: 是因为退出Activity时没有关闭弹出框,出现了这个错误 解决方法: 只需要在a ...
- bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token
========4 关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...
- 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传入,但是出了 andr ...
- android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic
之前遇到过这样的问题, 04-12 10:40:33.302: E/AndroidRuntime(17213): Caused by: android.view.WindowManager$BadTo ...
- 关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window
下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token ...
- 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 windo ...
- AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a
最近遇到一个奇葩的问题,好郁闷 之前也没有仔细看.问题偶尔出现一次.再去查看日志时,出现 view.WindowManager$BadTokenException: Unable to add win ...
- 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; @Ove ...
- Android报错:WindowManager$BadTokenException: Unable to add window -- window has already been added
很久之前测试通过的代码,现在手机升级了Android7.0后一运行就崩溃,报出这样的错误,具体错误如下: Process: com.example.sho.android_anti_theft, PI ...
随机推荐
- jQuer插件满屏气泡飘落动画效果
飘落动画效果插件引用: <script src="https://cdn.bootcss.com/JQuery-Snowfall/1.7.4/snowfall.jquery.min.j ...
- loadrunner 场景设计-集合点设置
场景设计-集合点设置 by:授客 QQ:1033553122 1 作用 通过让多用户在同一时间点上进行并发操作来测试系统的并发处理的能力 2 实现 通过集合点函数来实现. 注意:集合点经常和事务结 ...
- tomcat 取消项目名访问路径
在server.xml 里,<host>...</host>的标签之间添加<Context path="" docBase="projec ...
- 13.1、多进程:进程锁Lock、信号量、事件
进程锁: 为什么要有进程锁:假如现在有一台打印机,qq要使用打印机,word文档也要使用打印机,如果没有使用进程锁,可能会导致一些问题,比如QQ的任务打印到一半,Word插进来,于是打印出来的结果是各 ...
- MySQL8的注意点
最近使用MySQL8,发现两个问题,略记如下: 1. 新建用户无法使用JDBC或者Navicat等登陆,报错信息为 认证失败 ,原因为 新版 MySQL 认证插件变化(变为caching_sha2_p ...
- C# MD5 加密
public static string MD5Encrypt(string clearText) { string result = string.Empty; byte[] byteArray = ...
- Json Schema的使用
直接上案例: 在Web Api通讯中,客户端发送json数据,服务端反序列化json(json与某个类形成对应关系),在某些情况下,需要校验其上传的json是否合法. 服务端是使用Json.net(n ...
- WebDriverTest
using OpenQA.Selenium.Firefox; using System; using System.Collections.Generic; using System.Linq; us ...
- MySQL参数log_bin_trust_function_creators介绍-存储过程和复制
MySQL的有个参数log_bin_trust_function_creators,官方文档对这个参数的介绍.解释如下所示: log_bin_trust_function_creators Comma ...
- Deepin系统安装mysql教程及相关操作
Deepin系统安装mysql教程及相关操作 1.安装MySQL sudo apt-get install mysql-server,期间需要输入两次密码,root账户的密码. sudo apt-ge ...