Android 开发 Tip 5 -- View xxx not attached to window manager
来源:互联网 发布:美国手机音乐软件 编辑:程序博客网 时间:2024/06/04 01:28
转载请注明出处:http://blog.csdn.net/crazy1235/article/details/70195049
错误信息
java.lang.IllegalArgumentException: View=DecorView@f4179d7[title] not attached to window managerat android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:473)at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:382)at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:124)at android.app.Dialog.dismissDialog(Dialog.java:363)at android.app.Dialog.dismiss(Dialog.java:346)at com.jacksen.demo.view.alertdialog.TestAlertDialogActivity$1.run(TestAlertDialogActivity.java:78)at android.os.Handler.handleCallback(Handler.java:751)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:154)at android.app.ActivityThread.main(ActivityThread.java:6119)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这类错误一般是由于调用Dialog.dismiss()时,activity已经destory了。
常见场景一般都是在做异步操作(网络请求等)时,在回调函数中关闭对话框。
还原现场
progressDialog = ProgressDialog.show(this, "title", "loading...");MyHandler handler = new MyHandler(this);handler.postDelayed(new Runnable() { @Override public void run() { progressDialog.dismiss(); }}, 3000);handler.sendEmptyMessageDelayed(1, 1000);
private static class MyHandler extends Handler { private WeakReference<TestAlertDialogActivity> reference; public MyHandler(TestAlertDialogActivity activity) { this.reference = new WeakReference<TestAlertDialogActivity>(activity); } @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1 && reference.get() != null && !reference.get().isFinishing()) { reference.get().finish(); } } }
解决方案
知道了问题原因,解决也很简单。在dismiss的时候先判断activity是否还存在。但是此时也要注意另外一个问题。即是你判断了,也有可能出现另外一个错误,就是当你dismiss的时候,可能会出现 “xxx that was originally added here”!
所以最好的方式是在关闭activity的时候,及时调用dismiss来解除对ProgressDialog等Dialog的引用 !
1 0
- Android 开发 Tip 5 -- View xxx not attached to window manager
- android 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
- View not attached to window manager解决方案
- View not attached to window manager
- View not attached to window manager解决方案
- View not attached to window manager解决方案
- View not attached to window manager解决方案
- View not attached to window manager解决方案
- View not attached to window manager解决方案
- View not attached to window manager解决方案
- View not attached to window manager
- View not attached to window manager
- treed树形结构插件应用
- 编译安装caffe
- NYOJ611
- [LeetCode]225. Implement Stack using Queues
- Java序列化之readObjectNoData、readResolve方法
- Android 开发 Tip 5 -- View xxx not attached to window manager
- Mac安装homebrew介绍
- 那些TensorFlow上好玩的和黑科技
- 计算机程序语言与英语
- day55_电力项目_lucene在检索中的应用
- 3. Longest Substring Without Repeating Characters
- 183. Customers Who Never Order
- (转载)JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式)
- hdu1242(Rescue)深搜