bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token
========4 关于android的一个常见错误:Unable to add window --token is not valid
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41791b20 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:546)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:302)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:216)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:141)
at android.view.Window$LocalWindowManager.addView(Window.java:537)
at android.app.Dialog.show(Dialog.java:278)
at android.app.AlertDialog$Builder.show(AlertDialog.java:991)
at android.widget.TextView.onTouchEvent(TextView.java:8430)
at android.view.View.dispatchTouchEvent(View.java:5553)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2027)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2027)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1762)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1397)
at android.app.Activity.dispatchTouchEvent(Activity.java:2431)
at dalvik.system.NativeStart.main(Native Method)
1,错误分析:
从错误信息我们也可以明白其原因,此问题根本原因就是由于将要弹出的dialog所要依附的View已经不存在导致的。
2,什么地方可能照成此问题:当界面销毁后再弹出来;或者界面跳转时我们的view发生改变,dialog依附的context发生变化或者界面未运行了。
此外,很多时候我们需要通过一个非组件类来调用一个view类的方法来弹出dialog或Toast,这样就需要再提供一个静态context来创建这个dialog或者Toast
例如我们在一个view中通过一个静态类来弹出一个对话框:
AlertDialog.Builder builder = new AlertDialog.Builder(mContextNew);
当然并不是所有静态context都是可以用来创建dialog的,例如***App().getApplication().getApplicationContext()这个context就不行,因为它并不代表哪一个Activity或者View。。这样就无法add这个dialog。
此view用于绑定显示数据,我们在其构造方法中初始化一个静态变量mContextNew为此view的mContext。这样我们就可以通过一个静态类来弹出对话框了,只需传入这个静态的context(mContextNew)就可以了。。
但是这个静态的context如果只在构造方法中初始化的话是会存在问题的,因为如果另起了一个界面其绑定数据的view也是用的这个view那么这个静态context就会被重新修改。。
因此当这个新的界面finish后返回到上次的界面,这个静态的context是刚才已经finish的view的context。因此如果仍然传入这个静态变量通过一个静态类来弹出对话框就会出现上述找不到window的错误了。
解决办法:
对于tab页出现的错误可以用其父类的context来弹出dialog; 对于界面已经销毁引起的错误就只能判断界面是否存在然后再弹出了;
对于利用静态context来弹出的dialog可以通过规避的方式来解决,比如避免出现静态context被修改。。但是这样就可能限制了我们程序的功能。。
因此我们可以通过在bind数据时时时更新这个静态context就可以解决此问题了,这样就可以保证这个静态的context在任何view中都是当前的界面的view的context。就不会出现找不到其父类window了。
===== 3 android.view.WindowManager $ BadTokenException:无法添加窗口 - 令牌null是无效的; 为您的活动运行?
这里主要说的是你的android里一个Activity发生窗体泄漏了,也就是我们常说的内存泄漏,为什么窗体会泄漏,
主要是你的打开一个PopupWindow(窗体)时,如图。它没有关闭PopupWindow(窗体),就退出这个Activity,就会发生这个错误,
因为这里就有一个顺序,你要先关闭PopupWindow,再关闭Activity,这个一定的,PopupWindow(窗体)不能独立存在。
明白这个,你就容易解决了,你先用(dismiss)关闭就行,在你的窗体需要关闭时,加上这句:
(PopupWindow.dismiss;)PopupWindow这个是你项目里你窗体的名字。最好做一个判断,判断窗体是否为空,如果不为空就关闭,不然有时空也关闭也会出错。
========= 2 Button上点击一下弹出一个对话框,结果遇到一个问题,android.view.WindowManager$BadTokenException:Unable to add window报了这个错。
private void showCustomDialog() {
AlertDialog.Builder builder;
AlertDialog dialog;
LayoutInflater inflator = (LayoutInflater) LayoutInflatorActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflator.inflate(R.layout.dialoglayout, null);
TextView text = (TextView) view.findViewById(R.id.textview);
ImageButton imageButton = (ImageButton) view.findViewById(R.id.imageButton);
builder = new AlertDialog.Builder(this);
builder.setView(view);
dialog = builder.create();
dialog.show();
}
在这句上报错了。因为本来我的写法是:builder = new AlertDialog.Builder(this.getApplicationContext());
因为看了API 是new AlertDialog.Builder(Context context);想着也没什么语法错误吧。结果网上搜了一下,获取上下文this.getApplicationContext()); 和 this的区别:
这里的this指的当然就是Acitivity.this , 指的是这个Acitivity的上下文,而this.getApplicationContext()指的则是整个应用的上下文。
对于AlertDialog来说,是需要依赖一个View,而View是对应于Activity的。
那么为什么会报错呢,这里涉及到一个生命周期的问题了。
对于一个应用Context来说,它的生命周期是整个应用程序的生命周期,而对于Activity来说,当它销毁之后它的生命周期就结束了。
AlertDialog是属于Acitivity的,当Activity销毁的时候它也必须销毁, 所以这里我们指定是Activity的Context。
==========1 android.view.WindowManager$BadTokenException: Unable to add window
bug_ _ android.view.WindowManager$BadTokenException: Unable to add window -- token的更多相关文章
- 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 ...
- 关于ProgressDialog.show抛出android.view.WindowManager$BadTokenException: Unable to add window
下午摆弄ProgressDialog,进入就抛错:android.view.WindowManager$BadTokenException: Unable to add window -- token ...
- android.view.WindowManager$BadTokenException: Unable to add window
这是在加载dialog时出现的一个异常.转载地址:http://hi.baidu.com/fbdfp/item/7dea2d0ade9121813d42e23d 扔了好久的android又开始断断续续 ...
- 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 ...
- AsyncTask 与 对话框显示 view.WindowManager$BadTokenException: Unable to add window…is not valid; is your a
最近遇到一个奇葩的问题,好郁闷 之前也没有仔细看.问题偶尔出现一次.再去查看日志时,出现 view.WindowManager$BadTokenException: Unable to add win ...
- 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 ...
随机推荐
- hdu1811 并查集+拓扑序
题意:现在有一个排名系统,有一系列信息,分别是 > < = 的比较,而如果最终相等,就会将这些相等的按照序号从小到大排,问给出的信息是否可以确定完整的排序. 由于如果很多点相等,他们肯定能 ...
- Linux 的多线程编程的高效开发经验(转)
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/ 背景 Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多 ...
- C++ Unicode SBCS 函数对照表
C++ Unicode SBCS 函数对照表,以备日后查阅 Generic SBCS UNICODE TCHAR char wchar_t _TEOF EOF WEOF _TINT int wint_ ...
- C++面向对象要点
先说说面向对象思想的一个总体认识 对象通常会有行为,这些行为是靠信息支撑,这些信息包括外部信息和内部信息,对象行为会维护其中的一部分信息 因此对象可以看成是这样一种实体,它获取信息,然后决定自己的行为 ...
- JSBinding + SharpKit / 安装SharpKit以及添加SharpKit工程
本文说明如何往 sln 中添加 SharpKit 工程,以及配置. SharpKit 工程用于将 C# 源代码编译成 JS 代码. QQ群 189738580 1. 安装SharpKit 到 sha ...
- Oracle 12c RAC 搭建手册
1 共享设备配置 1.1 设备划分说明 冗余策略 卷划分及大小说明 OCRVOTING Ocrvoting01 8G Ocrvoting02 8G Ocrvoting03 8G ...
- 【android极光推送】—从客户端到后台,一文通吃
sion android:name="android.permission.VIBRATE" /> <uses-permission android:name=&quo ...
- createdb test时报global/pg_filenode.map不存在
实际上是存在的,看到说是ipv6占用了5432的端口号,看了一下的确是,将ipv6关闭重启系统即可. 下面是关闭ipv6的方法: 确认IPV6是否开启 在Linux下确认IPv6是否已经被启用,可以从 ...
- SQL2005中设置自动编号字段【转】
如果希望重新定义在表中添加新记录时该列中自动生成并存储于列中的序列号,则可以更改该列的标识属性.在每个表中只能设置一个列的标识属性. 具有标识属性的列包含系统生成的连续值,该值唯一地标识表中的每一行( ...
- Linux-remote change password (more)
1.creat managment Certification on Mangar Serverssh-keygen -t rsa2.creat client Certification on Cli ...