之前遇到过这样的问题,

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token

    ========4       关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...

  5. 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 ...

  6. 关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window

    下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token ...

  7. android.view.WindowManager$BadTokenException: Unable to add window

    这是在加载dialog时出现的一个异常.转载地址:http://hi.baidu.com/fbdfp/item/7dea2d0ade9121813d42e23d 扔了好久的android又开始断断续续 ...

  8. 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 ...

  9. popupwindow使用之异常:unable to add window -- token null is not valid

    使用popwindow中又碰到一个白痴问题,在此留作纪念,希望对大家有帮助 popupwindow之所以叫这个名字,肯定是要从某个地方弹出啦,但是从哪个地方呢?必须是指定一个view嘛 void an ...

随机推荐

  1. PL/SQL个人学习笔记(二)

    IF条件 declare cursor s is            select version from city_server t;   s_ city_server.version%type ...

  2. Bundle压缩JS和CSS

    ASP.NET MVC之Bundle压缩JS和CSS 介绍Bundle之前先引用<淘宝技术这十年>中一段话,对Web前端稍微有点常识的人都应该知道,浏览器下一步会加载页面中用到的CSS.J ...

  3. DDD分层架构之聚合

    DDD分层架构之聚合 前面已经介绍了DDD分层架构的实体和值对象,本文将介绍聚合以及与其高度相关的并发主题. 我在之前已经说过,初学者第一步需要将业务逻辑尽量放到实体或值对象中,给实体“充血”,这样可 ...

  4. WebStorm荣获InfoWorld2014年度科技奖

    InfoWorld年度科技奖是每年一月由InfoWorld评论家对过去一年的表现最好的信息产品的褒奖.产品包括硬件.软件.开发工具和云服务等. InfoWorld2014年度科技奖,包括35个获奖产品 ...

  5. LibVLC video controls

    原文 http://www.videolan.org/developers/vlc/doc/doxygen/html/group__libvlc__video.html VLC  3.0.0-git ...

  6. 因下面文的损坏或丢失windows/system32/config/system 解决方法

    这是因为你电脑的初始化文件遭破坏所致.导致破坏的原因也可能是病毒或其它原因. 因为Windows启动须要读取Syatem.ini,Win.ini和注冊表文件,假设C盘根文件夹下有config.sys, ...

  7. solrnet的使用

    solr与.net系列课程(五)solrnet的使用    solr与.net系列课程(五)solrnet的使用 最近因项目比较忙,所以这篇文章出的比较晚,离上一篇文章已经有半个月的时间了,这节课我们 ...

  8. 通过如何通过js实现复制粘贴功能

    在ie中window.clipboardData(剪切板对象)是可以被获取,所以利用这个方法我们可以实现在IE当中复制粘贴的功能,demo如下! <html> <head> & ...

  9. 测试驱动 ASP.NET MVC 和构建可测试 ASP.NET MVC 应用程序

    [测试驱动 ASP.NET MVC] http://t.cn/8kdi4Wl [构建可测试 ASP.NET MVC 应用程序]http://t.cn/8kdi4Wj

  10. 领域模型中分散的事务如何集中统一处理(C#解决方案)

    领域模型中分散的事务如何集中统一处理(C#解决方案)   开篇 什么是事务,事务的应用场景 做项目时,经常会遇到一些需求,比如注册用户时,要求同时存入用户的基本信息和初始化该用户的帐户,如果在这两个环 ...