Unable to add window --token is not valid

简介: Unable to add window --token is not valid

Process: com.android.******

Flags: 0x9be65

Package: com.android.*** v15 (4.0.4)

Build: ***:4.0.4/IMM76D/1348165925:eng/test-keys


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 com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)

at android.view.View.dispatchPointerEvent(View.java:5733)

at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3112)

at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2666)

at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:900)

at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2675)

at android.os.Handler.dispatchMessage(Handler.java:99)

at android.os.Looper.loop(Looper.java:154)

at android.app.ActivityThread.main(ActivityThread.java:4651)

at java.lang.reflect.Method.invokeNative(Native Method)

at java.lang.reflect.Method.invoke(Method.java:511)

at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)

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了。


相关文章
|
4月前
|
Java Spring
成功解决Initialization failed for ‘https://start.spring.io‘ Please check URL, network and proxy settings
这篇文章提供了解决Spring Initializr网站初始化失败问题的方法,包括检查URL、网络和代理设置。
成功解决Initialization failed for ‘https://start.spring.io‘ Please check URL, network and proxy settings
|
7月前
|
Java Spring
Initialization failed for 'https://start.spring.io' Please check URL, network and proxy settings.
Initialization failed for 'https://start.spring.io' Please check URL, network and proxy settings.
1154 0
|
Java Spring
Initialization failed for ‘https://start.spring.io‘ Please check URL, network and proxy settings解决办法
Initialization failed for ‘https://start.spring.io‘ Please check URL, network and proxy settings解决办法
3627 1
Initialization failed for ‘https://start.spring.io‘ Please check URL, network and proxy settings解决办法
|
网络安全 开发工具
【解决方案】A session ended very soon after starting. Check that the command in profile “XXX” is correct.
【解决方案】A session ended very soon after starting. Check that the command in profile “XXX” is correct.
1120 0
【解决方案】A session ended very soon after starting. Check that the command in profile “XXX” is correct.
|
Java
The type XXX cannot be resolved. It is indirectly referenced from required .class files
The type XXX cannot be resolved. It is indirectly referenced from required .class files
143 0
|
关系型数据库 MySQL 数据库
Your password has expired. To log in you must change it using a client that supports expired passwod
Your password has expired. To log in you must change it using a client that supports expired passwod错误解决
Your password has expired. To log in you must change it using a client that supports expired passwod
We cannot activate inspection type for article master in transaction code MM41?
We cannot activate inspection type for article master in transaction code MM41?
We cannot activate inspection type for article master in transaction code MM41?
|
Java 安全 网络安全
请求https错误: unable to find valid certification path to requested target
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/catoop/article/details/51155224 错误及原因 当Java客户端请求实现https协议的服务时,出现异常:’unable to find valid certification path to requested target’ 是因为服务期端的证书没有被认证,需要做的是把服务端证书导入到Java keystore。
8439 0
|
SQL 数据库管理 关系型数据库
SP2-0618: Cannot find the Session Identifier. Check PLUSTRACE role is enabled
测试环境:OEL6.5+Oracle 11g R2在进行执行计划测试的时候,遇到一个小问题。在用普通用户执行下面这条命令的时候,普通用户名为hhu,已经赋予了create session和resource权限。
1132 1