zoukankan      html  css  js  c++  java
  • View not attached to window manager crash 的解决办法

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

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


    down vote
    accepted
    +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);
            }
        }
    }
  • 相关阅读:
    linux异步信号handle浅析
    数据库的基本操作增删改查
    POJ1789Truck History最小生成树两种做法(Kruskal+Prim)模板题
    POJ1113Wall求凸包周长
    POJ3565AntsKM变形
    HDU2150Pipe判断线段是否相交
    POJ1815Friendship最大流最小割点+拆点+枚举
    HDU3081 Marriage Match II 最大匹配+并查集+匈牙利算法
    POJ3348Cows求凸包面积
    HDU3277Marriage Match III并查集+二分+最大流
  • 原文地址:https://www.cnblogs.com/cheneasternsun/p/5614323.html
Copyright © 2011-2022 走看看