使用Activity.isfinishing()解决java.lang.IllegalArgumentException: View not attached to window manager
2016-07-23 09:47
686 查看
在页面中使用了dialog、popwindow等弹框控件的时候,有时候会报java.lang.IllegalArgumentException: View not attached to window manager异常,怎么来的呢
看日志信息:
java.lang.IllegalArgumentException: View not attached to window manager
从上面的日志信息可以看出,报错的地方是dialog调用dismiss方法的时候。
java代码如下:
if (dialog != null) {
dialog.dismiss();
}
可以看到,dialog不为空,那么是怎么报的异常呢?随后我查阅了网上大多数的解决方法,总结了报错原因。这是由于因意外情况导致activity比dialog销毁的早,然后调用到dialog的方法时会发现没有activity依附,这一点从日志中就可以看出。
解决方法:
1、在页面的destory方法中,判断dialog是否存在,若存在,dismiss就销毁,使dialog的生命周期与activity同步;
2、在一些由于耗时操作或其他操作导致容易出现activity被销毁而dialog未销毁的地方,使用activity.isfinidhing方法判断activity是否被销毁,销毁,则终止代码;
PS:activity.isfinishing的官方API:
Check to see whether this activity is in the process of finishing, either because you called
has requested that it finished. This is often used in
finishing.
Returns
If the activity is finishing, returns true; else returns false.
翻译过来就是,isfinishing方法为true,当前页面销毁,反之亦然。
看日志信息:
java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:588) at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:325) at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:152) at android.app.Dialog.dismissDialog(Dialog.java:341) at android.app.Dialog$1.run(Dialog.java:123) at android.app.Dialog.dismiss(Dialog.java:326) at com.********.ShakeActivity1$2.processData(ShakeActivity1.java:197) at com*********.ShakeActivity1$2.processData(ShakeActivity1.java:1) at com.********.BaseActivity$BaseHandler.handleMessage(BaseActivity.java:201) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:4624) 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:965) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:732) at dalvik.system.NativeStart.main(Native Method)
从上面的日志信息可以看出,报错的地方是dialog调用dismiss方法的时候。
java代码如下:
if (dialog != null) {
dialog.dismiss();
}
可以看到,dialog不为空,那么是怎么报的异常呢?随后我查阅了网上大多数的解决方法,总结了报错原因。这是由于因意外情况导致activity比dialog销毁的早,然后调用到dialog的方法时会发现没有activity依附,这一点从日志中就可以看出。
解决方法:
1、在页面的destory方法中,判断dialog是否存在,若存在,dismiss就销毁,使dialog的生命周期与activity同步;
2、在一些由于耗时操作或其他操作导致容易出现activity被销毁而dialog未销毁的地方,使用activity.isfinidhing方法判断activity是否被销毁,销毁,则终止代码;
PS:activity.isfinishing的官方API:
Check to see whether this activity is in the process of finishing, either because you called
finish()on it or someone else
has requested that it finished. This is often used in
onPause()to determine whether the activity is simply pausing or completely
finishing.
Returns
If the activity is finishing, returns true; else returns false.
翻译过来就是,isfinishing方法为true,当前页面销毁,反之亦然。
相关文章推荐
- Android开发随手记录-AS导出jar包 Android Studio生成包含assets的Jar 从Eclipse迁移到Android Studio,并打包指定文件到jar
- 演示MP3的复制。通过IO流缓冲区。
- Java中删除文件、删除目录及目录下所有文件
- springmvc拦截器
- Eclipse乱码怎么办
- java21个知识点重点
- Java 文件和byte数组转换
- java随机数生成1到10 java怎么随机生成一个字符串?
- 开源 java CMS - FreeCMS2.5 标签 infoNextList
- springmvc上传文件
- Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exc
- java反射深入剖析(推荐)
- java之Map接口及其实现类
- Thinking in Java 第18章 Java I/O 系统(18.1-18.8)
- IO流拷贝图片
- Java逻辑运算符
- struts2标签Iterator迭代时获取下标、判断list是否为空
- 62. mybatis 使用PageHelper不生效【从零开始学Spring Boot】
- java发送qq邮件进行验证
- android studio出现Error:compileSdkVersion android-x requires compiling with JDK 7问题