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 ...
随机推荐
- React 入门学习笔记整理(五)—— state
1.state 1)组件本省也是有状态的,定义在组件内部的state中,state的状态只能由组件自身改变,任何其他组件都不能改变. 当需要改变state时,通过调用setState方法来改变,set ...
- 开发Hexo主题(一)
Hexo是一款静态博客工具,由Node.js编写 在博客根目录的themes下,新建文件夹(文件夹名为主题名) 主题目录结构如图 这里使用模版引擎为Ejs 在Layout目录下新建index.ejs文 ...
- python中常用函数整理
1.map map是python内置的高阶函数,它接收一个函数和一个列表,函数依次作用在列表的每个元素上,返回一个可迭代map对象. class map(object): ""&q ...
- SOAP REST
SOAP是基于RPC原理,是传统程序的函数调用和返回在RPC中被请求和应答代替了而已. SOAP Simple Object Access Protocol,是一种严格定义的信息交换协议,用于在web ...
- HTML 5 <input> list 属性
定义和用法 list 属性引用数据列表,其中包含输入字段的预定义选项. 可以用来做关联搜素
- Flask消息闪现
目录 Flask消息闪现 简单的例子 闪现消息的类别 过滤闪现消息 Message Flashing 参考 Flask消息闪现 一个好的应用和用户界面都需要良好的反馈.如果用户得不到足够的反馈,那么应 ...
- 【PAT】B1069 微博转发抽奖(20 分)
一开始并没有做出来,关键是没有认真理解题,每次做题之前都应该认真读题,自己把样例模拟一下,防止漏掉信息,减慢自己写代码的速度 此题的重点在于规划逻辑,以及如何储存中奖者,用map最好,否则查找并不方便 ...
- January 07th, 2018 Week 01st Sunday
To remember is to disengage from the present. 铭记过去就是放弃当下. To remember the past doesn't mean we would ...
- 【Beta Scrum】冲刺! 1/5
0. Alpha阶段遗留问题 项目 功能/页面 功能/页面 WEB端 图片在线编辑 文件上传跨域问题 app端 作业展示页面 1. Beta计划表 功能 说明 web端 登录 完成web端登录页面及功 ...
- python第五十二课--自定义异常类
myexception.py ''' 实现自定义异常类: ''' class MyException(Exception): def __init__(self,msg): super().__ini ...