java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{4380

转自:http://blog.csdn.net/yuxiaohui78/article/details/38076447

E/AndroidRuntime(9916): java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{43805410 V.E..... R.....ID 0,0-1026,288} not attached to window manager

E/AndroidRuntime(9916):  at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
E/AndroidRuntime(9916):  at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
E/AndroidRuntime(9916):  at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
E/AndroidRuntime(9916):  at android.app.Dialog.dismissDialog(Dialog.java:329)

E/AndroidRuntime(9916): at android.app.Dialog.dismiss(Dialog.java:312)

在将progressDialog 用在ASyncTask中的时候,有时候会遇到上面的错误。

我们先看一下,最常用的AsyncTask中使用ProgressDialog的方法。下面是最常用的使用方法。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class HttpRequestTask extends AsyncTask <String, Void, String> {  
  2.   
  3.     Context ctx = null;  
  4.     private ProgressDialog dialog = null;  
  5.   
  6.     public interface HttpRequestTaskListener {  
  7.         void ServerResponse (String jsonStr);  
  8.     }  
  9.   
  10.     public HttpRequestTask (final Context c){  
  11.           
  12.         ctx = c;  
  13.         dialog = new ProgressDialog(c);  
  14.       
  15.     }  
  16.   
  17.     public void addListener (HttpRequestTaskListener l){  
  18.         listeners.add(0, l);  
  19.     }  
  20.   
  21.         protected void onPreExecute() {  
  22.             dialog.setMessage("Downloading data from the Server...");  
  23.             dialog.show();  
  24.     }  
  25.       
  26.     @Override  
  27.     protected String doInBackground(String... params) {  
  28.         String url = params[0];  
  29.   
  30.         return HttpRequest ( url, .......);  
  31.     }  
  32.   
  33.     @Override  
  34.     protected void onPostExecute(String result) {  
  35.   
  36.         if (dialog.isShowing()) {  
  37.             dialog.dismiss();  
  38.         }  
  39.       
  40.             if (listeners.size() > 0){  
  41.                 listeners.get(0).ServerResponse ( result );  
  42.             }  
  43.         }  
  44.     }  

一般情况下,上面的方法不会出现任何错误。 任务结束后,ProgressDialog会正常消失。

但是在某些情况下,上面的使用方法就非常不安全。

在项目开发中会遇到在TabView的各个tab page进行切换。同时每个tab page中都会调用

这个异步任务HttpRequestTask去请求网络数据。这时会出现一个问题。
当用户在各个tab page间快速切换的时候,ProgressDialog 使用的context就很不安全。
会遇到调用 dialog.dismiss 和 dialog.show(); 的时候无法attach到Window Manager.

原因是在切换的时候,dialog还没有完成所有的调用,所对应的context已经被destroy或正在destroy。

这时就会导致上面的错误。

尝试解决这个问题。最初想使用ApplicationContext,(context.getApplicationContext()),但是,这个context无法用于ProgressDialog和Toast。会直接导致crash。

目前的解决方法是,先检查context对应的Activity的状态,如果不可用就停止dialog操作:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class HttpRequestTask extends AsyncTask <String, Void, String> {  
  2.   
  3.     Context ctx = null;  
  4.     private ProgressDialog dialog = null;  
  5.   
  6.     public interface HttpRequestTaskListener {  
  7.         void ServerResponse (String jsonStr);  
  8.     }  
  9.   
  10.     public HttpRequestTask (final Context c){  
  11.           
  12.         ctx = c;  
  13.         dialog = new ProgressDialog(c);  
  14.       
  15.     }  
  16.   
  17.     public void addListener (HttpRequestTaskListener l){  
  18.         listeners.add(0, l);  
  19.     }  
  20.   
  21.   protected void onPreExecute() {  
  22.         if (isValidContext(ctx)){  
  23.             dialog.setMessage("Downloading data from the Server...");  
  24.             dialog.show();  
  25.         }  
  26.     }  
  27.       
  28.     @Override  
  29.     protected String doInBackground(String... params) {  
  30.         String url = params[0];  
  31.   
  32.         return HttpRequest ( url, .......);  
  33.     }  
  34.   
  35.     @Override  
  36.     protected void onPostExecute(String result) {  
  37.   
  38.         if (isValidContext(ctx) && dialog.isShowing()) {  
  39.             dialog.dismiss();  
  40.         }  
  41.       
  42.             if (listeners.size() > 0){  
  43.                 listeners.get(0).ServerResponse ( result );  
  44.             }  
  45.         }  
  46.     }  
  47.   
  48.     private boolean isValidContext (Context c){  
  49.           
  50.         Activity a = (Activity)c;  
  51.           
  52.         if (a.isDestroyed() || a.isFinishing()){  
  53.             Log.i("YXH""Activity is invalid." + " isDestoryed-->" + a.isDestroyed() + " isFinishing-->" + a.isFinishing());  
  54.             return false;  
  55.         }else{  
  56.             return true;  
  57.         }  
  58.     }  

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值