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. kibana 统计每天注册数

  2. bootargs中的环境变量说明和一些常用的uboot命令

    bootargs中的环境变量说明和一些常用的uboot命令 一些常见的uboot命令:Help [command]在屏幕上打印命令的说明Boom [addr]启动在内存储器的内核Tftpboot通过t ...

  3. Uva227.Puzzle

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  4. [置顶] Objective-C ,ios,iphone开发基础:在UITextField输入完以后,隐藏键盘,

    在x-code Version 4.3.2 (4E2002)下编译: 在 Controller. m 文件下添加如下实例方法即可: - (void)viewDidUnload { [super vie ...

  5. jquery ajax 局部table 刷新技术

    点击查询:

  6. 人物角色群体攻击判定(三)Physics.OverlapSphere(群体攻击)

    使用Physics.OverlapSphere来检测不方便调试, 其他都可以.   核心代码: //检测敌人 public void CheckEnemy() { Collider[] cols = ...

  7. c++之 常用类型

    C/C++常用类型的范围 C/C++里常用的类型及表示范围如下表所示: 类型 sizeof 表示范围 说明 char 1 -128 - 127 -2^7 - (2^7 - 1) short 2 -32 ...

  8. 机房收费系统合作版(三)——UI思索

    案件追踪系统1.0暂告一段落.验收过程中.MR MI针对UI界面提出了很多自己的想法. 针对TGB项目的UI设计我也有我的感受: 1.不论大小项目.仅仅要一看界面准有70%到80%熟悉度. 2.一看这 ...

  9. POJ 3694 LCA

    题意:有N台电脑,他们之间有M条无向边. 然后询问,每次在他们之间加一条边,剩余的桥有多少. 思路:其实这题都不需要缩点了.. 直接记录每条桥的位置,然后每次询问进行一次LCA,当询问到桥时,桥数减1 ...

  10. contentSize、contentInset和contentOffset

    contentSize.contentInset和contentOffset 是 scrollView三个基本的属性. contentSize: The size of the content vie ...