Android中View not attached to window manager错误的解决办法
来源:互联网 发布:手机数据怎么连接电脑 编辑:程序博客网 时间:2024/04/27 17:00
原因基本都是Dismiss对话框的时候,Activity已经不再存在。
常发生这类Exception的情形都是,有一个费时的线程操作,需要在显示一个Dialog,在任务开始的时候显示一个对话框,然后当任务完成了再Dismiss对话框,如果在此期间如果Activity因为某种原因被杀掉且又重新启动了,那么当Dialog调用Dismiss的时候WindowManager检查发现Dialog所属的Activity已经不存在了,所以会报java.lang.IllegalArgumentException: View not attached to window manager
其实此类Exception的一重要的原因是,Dialog的创建显示和取消都允许在非UI线程中进程。在Android当中非UI线程是不允许操作UI相关的事情,比如添加移除View等,但是为会么允许创建显示和取消对话框呢?而且还有可能引发此Exception导致应用Crash。
要想避免此类Exception,就要正确的使用对话框,也要正确的使用线程。
正确的使用对话框
不要在非UI线程中使用对话框创建,显示和取消对话框。
那么对于异步操作显示对话框怎么办呢?Activity都有相应的操作对话框的回调比如onCreateDialog(),showDialog(),dimissDialog(),removeDialog()等等。这些因为都是Activity的方法,所以用起来更方便,也不用显示创建和操控Dialog对象,一切都由框架操控,相对来说比较安全。
另外就是一定要让对话框对象在Activity的可控制范围之内和生命周期之内,比如一定要是它的成员变量,并且在让对话框变量活跃在Activity的onCreate()和onDestroy()之间。
正确的使用线程
尽量少用单独线程,除非是真正的费时操作才用线程,线程也不要直接用Java式的匿名线程,除非是那种单纯的操作,操作完成后不需要做其他事情的。
尽可能多用Android提供的类比如AsyncTask等。另外如果线程操作过程中还需要与主线程有交互,那么最好保存一个线程的对象,并且线程内部最有一定的控制,这样可以让Activity更好的操控线程。
如果说某些操作是特别费时的,且是经常性的操作,比如从网络获取数据,或是从后台读取文件,或是导入/导出,恢复/备份的事情,最好放到后台Service中去做,然后在StatusBar中给出相应进度。
Android中 View not attached to window manager错误的解决办法
View not attached to window manager解决方案
- Android中 View not attached to window manager错误的解决办法
- Android中 View not attached to window manager错误的解决办法
- Android中View not attached to window manager错误的解决办法
- Android中 View not attached to window manager错误的解决办法
- Android中View not attached to window manager错误的解决办法
- Android中 View not attached to window manager错误的解决办法
- Android中 View not attached to window manager错误的解决办法
- Android中View not attached to window manager错误的解决办法
- Android中 View not attached to window manager错误的解决办法
- Android中View not attached to window manager错误,android.view.WindowLeaked的解决方案
- android View not attached to window manager
- android笔记之关于java.lang.IllegalArgumentException: View not attached to window manager 错误的分析
- Android Exception: View not attached to window manager
- Android View not attached to window manager解决方案
- view not attached to window manager
- View not attached to window manager
- illegalargumentexception: view not attached to window manager
- View not attached to window manager
- poj 1236 Network of Schools 强连通分量
- 职责链(Chain of Responsibility)模式
- S3C6410学习笔记汇总
- activity跳转动画
- Android GDI 图形渲染
- Android中View not attached to window manager错误的解决办法
- SDKD Single training #6未完成题目
- UVA 408 (13.07.28)
- UITableView简单解析
- poj2187
- xcode 4 制作静态库详解
- Java总结(22) 银行调度系统
- POJ 1328 Radar Installation
- Objective-C的内存管理机制