未连接到窗口管理器崩溃

未连接到窗口管理器崩溃

本文介绍了图中未连接到窗口管理器崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的ACRA报告应用程序崩溃。我得到一个查看未连接到窗口管理器错误信息,以为我已经通过包装 pDialog.dismiss()固定它; 中的if语句:

I am using ACRA to report app crashes. I was getting a View not attached to window manager error message and thought I had fixed it by wrapping the pDialog.dismiss(); in an if statement:

if (pDialog!=null)
{
    if (pDialog.isShowing())
    {
        pDialog.dismiss();
    }
}

它减少了查看未连接到窗口管理器崩溃我收到的数额,但我仍然得到了一些,我不知道如何解决它。

It has reduced the amount of View not attached to window manager crashes I recieve, but I am still getting some and I am not sure how to solve it.

错误消息:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:425)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:327)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:83)
at android.app.Dialog.dismissDialog(Dialog.java:330)
at android.app.Dialog.dismiss(Dialog.java:312)
at com.package.class$LoadAllProducts.onPostExecute(class.java:624)
at com.package.class$LoadAllProducts.onPostExecute(class.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
at dalvik.system.NativeStart.main(Native Method)

code片断:

Code snippet:

class LoadAllProducts extends AsyncTask<String, String, String>
{

    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        pDialog = new ProgressDialog(CLASS.this);
        pDialog.setMessage("Loading. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args)
    {
        // Building Parameters
        doMoreStuff("internet");
        return null;
    }


    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url)
    {
         // dismiss the dialog after getting all products
         if (pDialog!=null)
         {
                if (pDialog.isShowing())
                {
                    pDialog.dismiss();   //This is line 624!
                }
         }
         something(note);
    }
}

清单:

    <activity
        android:name="pagename.CLASS"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout"
        android:label="@string/name" >
    </activity>

我在想什么来阻止这种崩溃情况的发生?

What am I missing to stop this crash from happening?

推荐答案

如何重现bug:

  1. 启用您的设备上此选项:设置 - &GT;开发人员选项 - &GT;不要让活动
  2. preSS Home键,而的AsyncTask 正在执行和 ProgressDialog 是显示。
  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.

Android操作系统将尽快销毁活动,因为它是隐藏的。当 onPostExecute 活动将在整理的状态和 ProgressDialog 将不附活动

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.

如何解决这个问题:

  1. 检查你的 onPostExecute 法的活动状态。
  2. 辞退的 ProgressDialog 的onDestroy 方法。否则, android.view.WindowLeaked 将引发异常。此异常通常来自对话框仍处于活动状态时,活动结束。
  1. Check for the activity state in your onPostExecute method.
  2. Dismiss the ProgressDialog in onDestroy method. Otherwise, android.view.WindowLeaked exception will be thrown. This exception usually comes from dialogs that are still active when the activity is finishing.

试试这个固定的code:

Try this fixed code:

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);
        }
    }
}

这篇关于图中未连接到窗口管理器崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 19:49