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$BadTokenException: Unable to add window — token null is not for an application
04-12 10:40:33.302: E/AndroidRuntime(17213): at android.view.ViewRoot.setView(ViewRoot.java:509)
04-12 10:40:33.302: E/AndroidRuntime(17213): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
Google了很多,然后在StackOverflow中看了很多回答,发现基本上都是给出了解决方法,没有解释出什么太详细的原因。
后来找到了一篇文章,地址是
http://tech.shantanugoel.com/2010/07/08/badtokenexception-android-dialog-getapplicationcontext.html
关于错误的解释是这样的:
Solution: Just replace “getApplicationContext()” with “this” (i.e. “Context mContext = this;” ) and it will work fine.
解决方法:使用this替换掉getApplicationContext()即可正常使用。
Explanation: As to why this is exactly an issue, I’m a bit fuzzy about it myself but this much I’m sure that the contexts that you get with getApplicationContext and this are different. On reading about this function from Android SDK help:
Return the context of the single, global Application object of the current process. This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
getApplicationContext() 返回当前进程中单一的全部Application对象的上下文。该方法仅在你需要一个生命周期与当前上下文分离,并关联进程的生命周期而不是当前的部件。
I think this would mean is that getApplicationContext returns a context which is for the application itself and not the activity, while “this” would give you the context of the activity in which you are creating the dialog. I think since it is the activity which is associated with the UI (and for whom the window has been created), using the application context would have caused the crash here.
我认为上面的意思是getApplicationContext()返回一个针对与自身Application的上下文对象,而不是当前的Activity的上下文对象,而”this”,会返回一个你创建Dialog的Activity的上下文。因为Actvity是和UI相关的(并且窗口为它而创建),所以使用Application的上下文会直接崩溃。
更多参考 http://code.google.com/p/android/issues/detail?id=11199
android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an applic的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token
========4 关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...
- 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 ...
- 关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window
下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token ...
- android.view.WindowManager$BadTokenException: Unable to add window
这是在加载dialog时出现的一个异常.转载地址:http://hi.baidu.com/fbdfp/item/7dea2d0ade9121813d42e23d 扔了好久的android又开始断断续续 ...
- Unable to add window -- token null is not for an application错误的解决方法 android开发
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not f ...
- popupwindow使用之异常:unable to add window -- token null is not valid
使用popwindow中又碰到一个白痴问题,在此留作纪念,希望对大家有帮助 popupwindow之所以叫这个名字,肯定是要从某个地方弹出啦,但是从哪个地方呢?必须是指定一个view嘛 void an ...
随机推荐
- Junit指定测试运行顺序
原文链接: Test execution order原始日期: 2012年12月06日本期: 2014年7月2日翻译人员: 铁锚 说明: Junit4.11版本号及以后才支持,建议升级到最新版本号. ...
- Windows在结构objective C开发环境
对于近期打算iPhone.iPod touch和iPad开发一些应用程序,所以.需要开始学习Objective C(苹果推出的类似C语言的开发语言).因为苹果的自我封闭的产业链发展模式(从芯片.机器. ...
- Django小例子 – 模型数据的模板呈现
学习Django的这几天,学习过程还是很愉快的,django采用的MVC架构,学习曲线十分平缓,在深入学习之前,先简单的整理记录下django从数据库中获取数据并在模板中使用的方法.温故而知新 ^_^ ...
- 告别alert,拥抱console
记得学习javascript的第一个demo就是alert("Hello World");可是学习接触javascript这么长时间了还是在alert,因为javascript调 ...
- .Net编译之AnyCPU - 进阶者系列 - 学习者系列文章
Visual Studio是一款非常强大的IDE工具,它为我们提供了强大的编码.调试和测试等工具,为我们编好.NET软件提供了强大的支持. 今天早晨想到了Visual Studio编译模式中的CPU类 ...
- webapp 开发调试测试方法总结
好久都没有发表过日志了,反正近期项目也已经接近尾声了,那么是时候该总结一下在项目中用到的技术了,请看:这里先废话几句,我们现在的开发模式是这样子的:先把本地的网页上传到远程服务器(因为好多设备都要去访 ...
- windows下grunt安装提示不成功
在电脑按了node.js之后,在cmd中 输入 npm install -g grunt-cli 注意啦 不是 npm install -g grunt-cli 中间多了一个空格就安装不成功了,这个空 ...
- readonly和const的区别
readonly与const的区别1.const常量在声明的同时必须赋值,readonly在声明时可以不赋值2.readonly只能在声明时或在构造方法中赋值(readonly的成员变量可以根据调用不 ...
- openGL线s的绘制
/** * 缓冲区工具类 */ public class BufferUtil { /** * 将浮点数组转换成字节缓冲区 */ public static ByteBuffer arr2ByteBu ...
- Java类之间的关联关系(转载)
Java类之间的关联关系 UML类图中的关系分为四种:泛化.依赖.关联.实现:关联关系又可以细化为聚合和组合. 一.泛化(Generalization) 泛化是父类和子类之间的关系,子类继承父类的所有 ...