popupwindow使用之异常:unable to add window -- token null is not valid
2015-04-23 16:14
435 查看
最近用popupwindow出现一个问题,所以网上找了很多答案,这个最是齐全,所以粘贴上来供大家分享一下。
popupwindow之所以叫这个名字,肯定是要从某个地方弹出啦,但是从哪个地方呢?必须是指定一个view嘛
void android.widget.PopupWindow.showAtLocation(View parent, int gravity, int x, int y)
调用这个函数就能显示popupwindow了,但是有的同学会有幸碰到一个异常,关于这个异常的解释是unable to add window -- token null is not valid;is your activity running?
看起来就有点纳闷了,为啥呢?肯定是你showAtLocation第一个参数用不鸟啦,你有可能是在activity之外showAtLocation,或者是在Activity的onCreate()函数里面调用了showAtLocation,多半是后者,由于你的popupwindow要依附于一个activity,而activity的onCreate()还没执行完,哪来的popup让你弹出来嘛。
因此,你要做的就是让这个showAtLocation的调用再晚一点
解决办法如下:
popupwindow之所以叫这个名字,肯定是要从某个地方弹出啦,但是从哪个地方呢?必须是指定一个view嘛
void android.widget.PopupWindow.showAtLocation(View parent, int gravity, int x, int y)
调用这个函数就能显示popupwindow了,但是有的同学会有幸碰到一个异常,关于这个异常的解释是unable to add window -- token null is not valid;is your activity running?
看起来就有点纳闷了,为啥呢?肯定是你showAtLocation第一个参数用不鸟啦,你有可能是在activity之外showAtLocation,或者是在Activity的onCreate()函数里面调用了showAtLocation,多半是后者,由于你的popupwindow要依附于一个activity,而activity的onCreate()还没执行完,哪来的popup让你弹出来嘛。
因此,你要做的就是让这个showAtLocation的调用再晚一点
解决办法如下:
@Override public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); if(hasFocus){ showPopupWindow(View parent); } }以上方法运行到这个方法的时候系统才会肯定得告知你这个activity已经加载完毕。可以和用户进行交互了
相关文章推荐
- popupwindow使用之异常:unable to add window -- token null is not valid
- popupwindow使用之异常-Unable to add window -- token null is not valid; is your activity running?
- 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? -
- popupwindow异常:unable to add window — token null is not valid
- PopupWindow出现Unable to add window -- token null is not valid; is your activity running?
- 使用PopWindow遇到的BadTokenException: Unable to add window is not valid; is your activity r
- Android开发之关于“Unable to add window - token null is not for an application ”的异常
- Unable to add window -- token null is not valid
- Unable to add window -- token null is not valid
- unable to add window token null is not valid
- Unable to add window -- token null is not valid; is your activity running?错误及其修改方法
- android使用AlertDialog提示 Unable to add window -- token null is not for an application的解决方案
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is yo
- AndroidException -Unable to add window -- token null is not valid;
- Unable to add window -- token null is not valid; is your activity running?
- popwindow初始时弹出引发的Unable to add window --token null is not valid
- unable to add window -- token null is not valid; is your activity running 错误解决办法
- PopupWindow出现Unable to add window -- token null is not valid; is your activity running?
- android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid
- 使用软引用解决Handler内存泄露和显示Popupwindow、Dialog时提示"Unable to add Window-token is null"的问题