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 ...
随机推荐
- leetCode练题——38. Count and Say
1.题目 38. Count and Say The count-and-say sequence is the sequence of integers with the first five te ...
- pycharm/IDEA等 windows 版 常用快捷键
- ES6-let声明变量
在es6中除了var还可以用let申明变量,并且建议使用let而不要再使用var,两者有以下区别: 1.let不能重复声明变量 var name = 'tom'; var name = 'jack'; ...
- 思科Catalyst 9K
思科的新一代产品Catalyst9K,里面涉及了Catalyst9200.Catalyst9300.Catalyst9400.Catalyst9500.Catalyst9600和Catalyst980 ...
- location练习!(重点)
写location最重要的就是hosts文件,添加一次域名跳转就得添加一条hosts文件 hosts文件: 192.168.200.120 www.a.com 192.168.200.119 www. ...
- WEB - token
token概念参考 https://ninghao.net/blog/2834 https://stackoverflow.com/questions/1592534/what-is-token-ba ...
- 一文解读RISC与CISC (转)
RISC(精简指令集计算机)和CISC(复杂指令集计算机)是当前CPU的两种架构.它们的区别在于不同的CPU设计理念和方法. 早期的CPU全部是CISC架构,它的设计目的是要用最少的机器语言指令来完成 ...
- SpringBoot与Mybatis整合,插件生成dao、mapper、pojo
一.创建SpringBoot项目,引入相关依赖包 <?xml version="1.0" encoding="UTF-8"?> <projec ...
- 夯实Java基础(二十四)——Java8新特征之Optional类
1.概述 对于Java程序员来说,到目前为止出现次数最多的应该是NullpointException,它是导致Java应用程序失败的最常见原因.之前处理空指针我们必须先通过条件先去判断,然后再确认是否 ...
- Linux centosVMware shell编程 for循环、while循环、break跳出循环、continue结束本次循环、exit退出整个脚本
一.for循环 语法:for 变量名 in 条件; do …; done 案例1 #!/bin/bash sum=0 for i in `seq 1 100` do sum=$[$sum+$i] ec ...