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 ...
随机推荐
- 初学HTML-10
marquee标签:设置文字滚动效果. 格式:<marquee>文字滚动</marquee> 属性:direction:设置滚动方向:left / right / up / d ...
- JQuery的事件委托;jQuery注册事件;jQuery事件解绑
一.事件 ①事件委托:就是给子元素的父元素或者祖先元素注册一个事件,但是事件的执行者是子元素,委托事件的好处是能够给动态创建出来时元素也加上事件. ②简单事件:就是给自己注册事件自己执行动态创建出来的 ...
- 使用 NGINX 流控和 fail2ban 防止 CC 攻击
背景知识 CC 攻击 攻击者通过创建大量请求导致服务器资源耗尽,主要针对特定服务接口,属于实现 DoS 攻击的一种方式(DoS 攻击更多是针对网络端口,而不是具体服务接口). NGINX 流控 lim ...
- LeetCode题解之Binary Tree Level Order Traversal II
1.题目描述 2.题目分析 先遍历,再反转. 3.代码 vector<vector<int>> levelOrderBottom(TreeNode* root) { vecto ...
- 基于TensorFlow进行TensorBoard可视化
# -*- coding: utf-8 -*- """ Created on Thu Nov 1 17:51:28 2018 @author: zhen "&q ...
- [20180612]删除bootstrap$记录无法启动.txt
[20180612]删除bootstrap$记录无法启动.txt --//前几天看链接http://www.xifenfei.com/2018/05/willfully-delete-bootstra ...
- python第三十七天--异常--socket
异常处理 #!usr/bin/env python #-*-coding:utf-8-*- # Author calmyan list_l=['g','h'] data={'} try: #list_ ...
- SELinux 是什么?
一.SELinux的历史 SELinux全称是Security Enhanced Linux,由美国国家安全部(National Security Agency)领导开发的GPL项目,它拥有一个灵活而 ...
- Cocoapods的安装和使用(2018-08-07更新)
一.Cocoapods的安装 第一步:打开终端 第二步:修改ruby镜像引用 gem source --remove https://rubygems.org/ gem sources -a http ...
- Ubuntu18.04 安装tomcat9
1.官网下载 2.移动到/usr/local/tomcat 3.解压 4.修改权限,否则在idea中不能正常使用