popupwindow使用之异常:unable to add window -- token null is not valid
使用popwindow中又碰到一个白痴问题,在此留作纪念,希望对大家有帮助
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的调用再晚一点,下面网上找个一个解决方案,我把我的代码直接贴吧,还是代码可靠,神马都是bullshit!在activity类中添加handler
private Handler popupHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
popupWindow.showAtLocation(findViewById(R.id.rlShowImage), Gravity.CENTER|Gravity.CENTER, 0, 0);
popupWindow.update();
break;
}
} };
然后再在onCreate()函数中添加
popupHandler.sendEmptyMessageDelayed(0, 1000);
也就是增加一个延时的任务而已啦,简单有效。
android AlertDialog显示错误 Unable to add window token null is not for an application解决办法
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(getApplicationContext())
.setTitle(R.string.dialogTitle)
.setMessage(R.string.dialogMessage)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "你按了确定。", Toast.LENGTH_SHORT).show();
}
}).show();
}
});
}
报错:Unable to add window token null is not for an application
异常原因:AlertDialog创建语句public AlertDialog.Builder (Context context)中,不能使用getApplicationContext()得到的context,而必须使用Activity,所以解决如下
解决办法:
将 new AlertDialog.Builder(getApplicationContext()) 改为 new AlertDialog.Builder(MainActivity.this)
其中MainActivity 为当前Activity的名称
popupwindow使用之异常:unable to add window -- token null is not valid的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Unable to add window -- token null is not for an application
导致报这个错是在于new AlertDialog.Builder(mcontext),虽然这里的参数是AlertDialog.Builder(Context context)但我们不能使用getApp ...
- 自定义对话框 提示:Unable to add window token null is not for an application
这是因为在new Dialog(context);的时候传入的context是通过getApplicationContext()获得的,这样就会报错. 把context的获得方式改为MainActiv ...
- bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token
========4 关于android的一个常见错误:Unable to add window --token is not valid android.view.WindowManage ...
随机推荐
- java并发基础知识
这几天全国都是关键时候,放假了,还是要学习啊!很久没有写博客了,最近看了一本书,有关于java并发编程的,书名叫做“java并发编程之美”,讲的很有意思,这里就做一个笔记吧! 有需要openjdk8源 ...
- 初探three.js几何体-Geometry
three.js几何体我们还没有说完,这一节我们说一说THREE.Geometry(),简单几何体都是继承了这个对象,使用它会相对麻烦一些,但是可操作性非常高,今天我们使用它制作一个自定义几何体-五角 ...
- Android 调起系统相机拍照
概述 最近在看 nanChen 写的图片选择器 ImagePicker,感觉写得很不错,也打算把从中学到的东西写下来.很多时候,遇到一个好的框架能够降低开发成本这是好事.但是也要去了解其内部具体实现逻 ...
- Web--Response
using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using ...
- kubernetes的Ingress资源介绍
Ingress 的资源帮助信息介绍 [root@master ~]# kubectl explain ingress KIND: Ingress VERSION: extensions/v1beta1 ...
- 吴裕雄 Bootstrap 前端框架开发——简介
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- UDLD(Unidirectional Link Detection)
1.UDLD(单向链路检测协议)工作原理 为了在生成转发环路之前检测到单向链路,Cisco 设计并实施了 UDLD 协议.UDLD 是与第 1 层 (L1) 机制一起工作以确定链路物 ...
- 关于转入软件工程专业后第二次java课上作业的某些体会
今天是第二周的java课. 自从转入了软件工程专业后,在我没有学习c++的基础上,直接开始了学习java的过程.不得不说过程很艰辛.今天下午老师让编写一个随机产生作业的软件.而我的基础差到都不知道如何 ...
- 安装pytorch
安装cpu版本的 conda install pytorch-cpu torchvision-cpu -c pytorch 安装gpu版本的 conda install pytorch torchvi ...
- yp寒假训练一
19年东北四省省赛 做了J G C 补了E H J签到题 G 题意: 给n个正方形的两个斜对角点坐标,问最小的移动可以重叠(移动上下左右一格) 思路: 一开始想的是中心pos移动,但是可能有小数,而且 ...