android.view.WindowManager$BadTokenException:Unable to add window --token is not valid; is your acti
来源:互联网 发布:上烈士墙的淘宝排行榜 编辑:程序博客网 时间:2024/05/17 07:14
异常信息:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
问题描述:在Activity的onResume()方法中起一个线程,并在线程结束的时候弹出一个popupwindow,但是在这段代码处却报上面的异常:
popupwindow.showAtLocation(findViewById(android.R.id.content), Gravity.CENTER, 0,0);
打印findViewById(android.R.id.content)却不是空,网上查找资料,说原因是Activity的控件还没有渲染完,可以通过以下方法来解决这个问题:
findViewById(android.R.id.content).post(new Runnable() {@Overridepublic void run() {popupWindow.showAtLocation(findViewById(android.R.id.content), Gravity.CENTER, 0,0);}});
这里虽然new了一个Runnable接口,但是run()方法中的代码却是在主线程执行的,这段代码的作用,就是把popupwindow弹出这段代码加到主线程的消息队列中去,stackoverflow中答案的作者说:这样就能等控件渲染完,再弹出popupwindow。这个方法确实解决了我的问题。
这种方法在弹出Dialog时,同样适用
以下几点是我的疑问:
1、android Activity的生命周期中,在onStart()方法之后,Activity就是visiable状态,那么为什么在onResume()中启动线程,然后弹出popupwindow,这个时候Activity的控件为什么还没有渲染完?
2、view.post()方法只是把消息放到主线程的消息队列中,为什么就会在Activity中的控件渲染完成之后再执行popupwindow弹出那段代码?
3、这个问题只在5.1.1系统的一个机器上出现了,但是在低版本的系统中却没有出现。
问题地址:http://stackoverflow.com/questions/4187673/problems-creating-a-popup-window-in-android-activity
相关问题:“android.view.WindowManager$BadTokenException: Unable to add window — token null is not for an application”
- android.view.WindowManager$BadTokenException:Unable to add window --token is not valid; is your acti
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is yo
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is yo
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is yo
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid
- view.WindowManager$BadTokenException: Unable to add window…is not valid; is your activity running?
- android spinner点击android.view.WindowManager$BadTokenException: Unable to add window token is not va
- android spinner点击android.view.WindowManager$BadTokenException: Unable to add window token is not va
- android spinner点击android.view.WindowManager$BadTokenException: Unable to add window token is not va
- Android报错android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an appli
- 关于android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an app
- 网络层
- the behavior of the UICollectionViewFlowLayout is not defined because:
- 页面中字符串超出后的解决方案
- RAC 的一些概念性和原理性的知识
- linux命令之stat,file,less
- android.view.WindowManager$BadTokenException:Unable to add window --token is not valid; is your acti
- Mac Terminal 快捷键
- Oracle如何限制固定IP的访问
- Repository模式中,Update和Delete总是失败及其解析(报错“another entity of the same type already has the same primar”)
- AFNetworking
- android死机LOG记录
- CSDN-markdown编辑器使用指南
- 开源库实现字体闪烁功能
- 通过StackTraceElement获取方法调用者的具体信息