View not attached to window manager crash 的解决办法

转自:http://stackoverflow.com/questions/22924825/view-not-attached-to-window-manager-crash

down voteaccepted

+50

How to reproduce the bug:

  1. Enable this option on your device: Settings -> Developer Options -> Don't keep Activities.
  2. Press Home button while the AsyncTask is executing and the ProgressDialog is showing.

The Android OS will destroy an activity as soon as it is hidden. When onPostExecute is called the Activity will be in "finishing" state and the ProgressDialog will be not attached to Activity.

How to fix it:

  1. Check for the activity state in your onPostExecute method.
  2. Dismiss the ProgressDialog in onDestroy method. Otherwise, android.view.WindowLeakedexception will be thrown. This exception usually comes from dialogs that are still active when the activity is finishing.
public class YourActivity extends Activity {

    <...>

    private void showProgressDialog() {
if (pDialog == null) {
pDialog = new ProgressDialog(StartActivity.this);
pDialog.setMessage("Loading. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
}
pDialog.show();
} private void dismissProgressDialog() {
if (pDialog != null && pDialog.isShowing()) {
pDialog.dismiss();
}
} @Override
protected void onDestroy() {
dismissProgressDialog
();
super.onDestroy();
} class LoadAllProducts extends AsyncTask<String, String, String> { /**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
showProgressDialog();
} /**
* getting All products from url
* */
protected String doInBackground(String... args)
{
doMoreStuff("internet");
return null;
} /**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url)
{
if (YourActivity.this.isDestroyed()) { // or call isFinishing() if min sdk version < 17
return;
}
dismissProgressDialog();
something(note);
}
}
}

View not attached to window manager crash 的解决办法的更多相关文章

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

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

  2. 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 ...

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

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

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

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

  5. View not attached to window manager

    java.lang.IllegalArgumentException: View not attached to window manager 在用ProgressDialog的时候,任务结束后Dis ...

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

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

  7. 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 ...

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

    在跑Monkey测试的时候出现了一个比较特别的问题,先来看看Log: // CRASH: com.meizu.media.painter (pid 12491) // Short Msg: java. ...

  9. view not attached to windows manager与This Toast was not created with Toast.makeText()

      http://blog.sina.com.cn/s/blog_474928c90100x871.html     public class Ex04_1Activity extends Activ ...

随机推荐

  1. 【转】vim文件编码和乱码处理

    原文网址:http://edyfox.codecarver.org/html/vim_fileencodings_detection.html 在 Vim 中,有四个与编码有关的选项,它们是:file ...

  2. errno的基本用法

    error是一个包含在 perror()和strerrot()函数可以把errno的值转化为有意义的字符输出. #include <stdio.h> #include <stdlib ...

  3. UNIX时间戳与日期的相互转换

    mysql中UNIX时间戳与日期的相互转换 UNIX时间戳转换为日期用函数:FROM_UNIXTIME() select FROM_UNIXTIME(1410318106); 日期转换为UNIX时间戳 ...

  4. Cocoa Pods的安装

    CocoaPods是用Ruby实现的,要想使用它首先需要有Ruby的环境.幸运的是OS X系统默认已经可以运行Ruby了,因此我们只需执行以下命令: sudo gem install -n /usr/ ...

  5. iOS8 Core Image In Swift:更复杂的滤镜

    iOS8 Core Image In Swift:自动改善图像以及内置滤镜的使用 iOS8 Core Image In Swift:更复杂的滤镜 iOS8 Core Image In Swift:人脸 ...

  6. phonegap环境配置与基本操作

    一.开发环境配置: 1.工具环境安装: 安装java sdk 1.6以上版本号,Android Development Tools.ant,系统变量 Path后面加入 新增名稱 JAVA_HOME 值 ...

  7. poj1036-dp

    http://poj.org/problem?id=1036 题目分析: 由题目很容易就能想到这道题目是DP题目. 当然,它的DP方程也不难得到: 定义状态:d[i,j] 表示在时间t=i且门状态为j ...

  8. Linux 编译安装 apache 2.4

    在安装apache之前需要准备一些必要的依赖包 gcc安装: #yum install -y gcc gcc-c++安装: #yum install gcc-c++  apr安装: 下载包:apr-1 ...

  9. NFinal简介

    NFinalWeb框架是基于魔法糖语法思想创建的框架.本框架有两大特点.1.所有框架里最简单易学易配置的.2.所有框架里运行效率最快的. 相关介绍1.运行效率比任何php和java以及.net框架要快 ...

  10. 0118——UIButtton

    1.Button的定义 UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; Button有六种类型 enum { UI ...