在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log:

 // CRASH: com.meizu.media.painter (pid 12491)

 // Short Msg: java.lang.IllegalArgumentException

 // Long Msg: java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{21dd6762 V.ED.... R......D 0,0-538,105} not attached to window manager

 // Build Label: ***

 // Build Changelist: 1443062570

 // Build Time: 1443062722000

 // java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{21dd6762 V.ED.... R......D 0,0-538,105} not attached to window manager

 // at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:416)

 // at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:342)

 // at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)

 // at android.app.Dialog.dismissDialog(Dialog.java:354)

 // at android.app.Dialog.dismiss(Dialog.java:337)

 // at com.***.***.***.PainterDrawActivity.closeProgressDialog(PainterDrawActivity.java:60)

 // at com.***.***.painter.PainterDrawActivity.access$000(PainterDrawActivity.java:28)

 // at com.***.***.painter.PainterDrawActivity$1$2.run(PainterDrawActivity.java:49)

 // at android.os.Handler.handleCallback(Handler.java:815)

 // at android.os.Handler.dispatchMessage(Handler.java:104)

 // at android.os.Looper.loop(Looper.java:194)

 // at android.app.ActivityThread.main(ActivityThread.java:5741)

 // at java.lang.reflect.Method.invoke(Native Method)

 // at java.lang.reflect.Method.invoke(Method.java:372)

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

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

 //

从上面的Log可以看出是一个非法参数异常的问题。

那为什么会出现Long Msg: java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{21dd6762 V.ED.... R......D 0,0-538,105} not attached to window manager的问题呢?应用本身的崩溃怎么会联系到

PhoneWindow呢?

(1). 为什么报错com.android.phone已停止运行?

       通过查看网上资料可以知道,在原码中在其AndroidManifest.xml可以发现:android:sharedUserId="android.uid.phone"也就是和Phone在一个进程中,因此在报错的表现上来讲就会是PhoneWindow。

(2). 为什么会报View not attached to window manager错误?

       这个错误的意思是说我们所操作的View没有被纳入window manager的管理。
       我 们知道所有的窗口创建和管理都是依附于window manager的,因此Dialog的创建也不例外。Dialog的创建流程通过查看源码可以知道,在Dialog的构造函数中,创建了一个Window 对象,但我们知道Window对象并不是用于显示的,真正用于显示的是View对象。因此通过Dialog的show方法构造了一个mDecor的 View对象,并最终通过WindowManager的addView()方法显示Dialog。
       通过查看log信息我们可以看到PainterDrawActivity.closeProgressDialog(PainterDrawActivity.java:60)查看对应的closeProgressDialog代码后发现,在60行处代码为dialog.dismiss();
       在网络上搜索后发现,多数情况下出现这种错误,都是在dismiss Dialog时,发现创建该Dialog的Activity存在而导致的。
比如在界面上显示一个 Dialog,当任务处理结束后再Dismiss Dialog。如果在Dialog显示期间,该Activity因为某种原因被杀掉且又重新启动了,那么当任务结束时,Dismiss Dialog的时候WindowManager检查,就会发现该Dialog所属的Activity已经不存在了(重新启动了一次,是一个新的 Activity),所以会报IllegalArgumentException: View not attached to window manager.
通过以上分析我们可以知道在Dialog在执行dismiss方法时,发现启动它的Activity已经不见了,被杀掉了(现在这个是重新启动的),所以才报错出现异常。

(3). 为什么Activity会被"杀掉"?

由于是Monkey测试出现一次的Log,我们很难复现,只能从网上遇到类似问题的人看看他们的看法。

对于onSaveInstanceState方法,在Android SDK里面有这样的描述:Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key)
也就说当某个activity变得“容易”被系统销毁时,该activity的onSaveInstanceState就会被执行,除非该activity是被用户主动销毁的,例如当用户按BACK键的时候。注意这里的容易二字,当前Activity并没有被销毁,只是系统觉得它有可能会被销毁因此会执行该方法。在该方法中我们可以保存Activity中的各种 数据信息,如果该Activity真的被杀掉而又重新启动后,可以使用onRestoreInstanceState方法在重新启动该Activity 时,还原我们之前保存的数据信息。onSaveInstanceState方法的调用遵循一个重要原则,即当系统“未经你许可”销毁了你的 Activity时,onSaveInstanceState就会被系统调用,这是系统的责任,因为它必须要提供一个机会让你保存你的数据(当然如果你自 己不保存,那就没法恢复了)。
       因此通过以上分析我们可以看到Activity的确被杀掉后再次启动了。

(4). 在SActivity被杀掉时,Dialog存在么?

       可能大家会有疑问,Dialog都没有看到,就出现错误了,怎么能确定该Dialog当时一定是显示的呢?
       其实Activity在被销毁时,其所依附的Dialog是存在的。

(5). 如何解决这个问题呢?

       通过以上分析之后我们知道了问题出现的原因,那么如何解决呢?可以通过以下两个方面来解决:

1. 使用Activity自带的Dialog控制方法

       在 Activity中需要使用对话框,可以使用Activity自带的回调,比如 onCreateDialog(),showDialog(),dimissDialog(),removeDialog()等等。毕竟这些都是 Activity自带的方法,所以用起来更方便,也不用显示创建和操控Dialog对象,一切都由框架操控,相对来说比较安全。

2. 限制Dialog的生命周期

       让创建的Dialog对象的存活周期跟Activity的生命周期一致,也就是说Dialog的生命周期被限定在Activity的onCreate()和onDestroy()方法之间。
 
 

关于dialog引起的 java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView not attached to window manager 错误的分析的更多相关文章

  1. 关于java.lang.IllegalArgumentException: View not attached to window manager 错误的分析

    今天遇到一个很奇特的问题,当用户设置了PIN码,在锁屏界面正常解锁PIN码后,进入Launcher时显示com.android.phone 已停止运行.一开始猜想会不会是解锁PIN码的时候处理导致了P ...

  2. android在学习——activity关闭和dialog.dismiss冲突的解决(Activity has leaked window com.android.internal.policy.impl.PhoneWindow)

    当我们在退出整个程序的时候偶尔会出现这种报错:Activity has leaked window com.android.internal.policy.impl.PhoneWindow 其意思大概 ...

  3. decorview that was originally added here or java.lang.IllegalArgumentException: View not attached to window manager

    使用Dialog的时候,没少出现下面这两个报错 12-11 17:47:49.776: E/WindowManager(11461): android.view.WindowLeaked: Activ ...

  4. java.lang.IllegalArgumentException: View not attached to window manager

    公司项目线上bug: java.lang.IllegalArgumentException: View not attached to window manager at android.view.W ...

  5. 继承映射中的java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: person is not mapped [FROM person]

    继承映射中查询对象的过程中报错: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxExcep ...

  6. java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: student is not mapped

    Spring 5.0 +Jpa,使用@Query实现 自定义查询报错: java.lang.IllegalArgumentException: org.hibernate.hql.internal.a ...

  7. Android中 View not attached to window manager错误的解决办法

    前几日出现这样一个Bug是一个RuntimeException,详细信息是这样子的:java.lang.IllegalArgumentException: View not attached to w ...

  8. bug_ _java.lang.IllegalArgumentException: View not attached to window manager 2

    今天遇到一个很奇特的问题,当用户设置了PIN码,在锁屏界面正常解锁PIN码后,进入Launcher时显示com.android.phone 已停止运行.一开始猜想会不会是解锁PIN码的时候处理导致了P ...

  9. bug_ _java.lang.IllegalArgumentException: View not attached to window manager

    ============= 1   view not attached to window manager 转自:http://hi.baidu.com/spare_h/blog/item/7fa3e ...

随机推荐

  1. html5 svg 第八章 文字text

    虽然它可能是真实的,每一个画面讲述了一个故事,这是完全正确的,用言语来帮助讲故事.因此,SVG有几个元素,让你将文本添加到您的图形. 文本术语 Text Terminology 在我们调查的主要方法添 ...

  2. C# 线程知识--使用ThreadPool执行异步操作

    C# 线程知识--使用ThreadPool执行异步操作 在应用程序中有许多复杂的任务,对于这些任务可能需要使用一个或多个工作线程或I/O线程来协作处理,比如:定时任务.数据库数据操作.web服务.文件 ...

  3. http://www.cnblogs.com/eye-like/p/4121219.html

    c# 操作Word总结 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想到网页的信息创建到一个word文本中, ...

  4. ReentrantReadWriteLock读写锁的使用

    Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. 读写锁:分为读 ...

  5. Failed to load unit 'PATM' (VERR_SSM_FIELD_NOT_CONSECUTIVE)

    今天打开虚拟机启动的时候报错:Failed to load unit 'PATM' (VERR_SSM_FIELD_NOT_CONSECUTIVE) 后来发现虚机处于休眠状态,所以在虚机上右键,然后清 ...

  6. 安装qc 出现error An error occurred while attempting to connect to the database.

    When trying to install mercury quality center starter edition 9.0 on Windows XP media center, I am g ...

  7. codevs 3160 最长公共子串(SAM)

    3160 最长公共子串   题目描述 Description 给出两个由小写字母组成的字符串,求它们的最长公共子串的长度. 输入描述 Input Description 读入两个字符串 输出描述 Ou ...

  8. 使用Windows Azure创建Windows系统虚拟机-上

    创建虚拟机来运行Windows 本教程介绍了如何轻松创建运行Windows 的 Azure虚拟机(VM),用作来自Azure管理门户中映像图库的Windows 服务器映像.映像图库提供了多种图像,包括 ...

  9. Script.NET Perl解释器代码已经在GitHub开源发布

    Script.NET Perl解释器的代码已经提交到GitHub网站.GitHub项目地址: https://github.com/blueantst/Script.NET Perl解释器代码在Src ...

  10. MySQL 5.7 for Windows 解压缩版 MySQL 服务无法启动

    MySQL 5.7 for Windows 解压缩版配置安装 http://jingyan.baidu.com/article/f3ad7d0ffc061a09c3345bf0.html basedi ...