【问题标题】:View not attached to window manager crash视图未附加到窗口管理器崩溃
【发布时间】:2014-05-20 10:16:55
【问题描述】:

我正在使用 ACRA 报告应用程序崩溃。我收到了 View not attached to window manager 错误消息,并认为我已经通过将 pDialog.dismiss(); 包装在 if 语句中来修复它:

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

它减少了我收到的View not attached to window manager 崩溃的数量,但我仍然遇到一些问题,但我不知道如何解决。

错误信息:

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)

代码sn-p:

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>

为了阻止这种崩溃的发生,我缺少什么?

【问题讨论】:

  • 你有想过这个吗?我有同样的问题。似乎无法弄清楚。
  • 很遗憾没有。稍后我可能会开始赏金。您应该查看其他一些处理此类问题的线程,以防它们对您有所帮助。
  • 您的AsyncTaskActivityFragment 中声明?
  • 请发布函数“doMoreStuff()”和“something()”。
  • 这个问题可能是因为主线程的工作量太大,所以尝试使用handlers来显示progressdialog,如果(pDialog!=null)这行是不需要的,因为isShowing自己检查是否对话是否正在进行中。

标签: android


【解决方案1】:

如何重现错误:

  1. 在您的设备上启用此选项:Settings -&gt; Developer Options -&gt; Don't keep Activities
  2. AsyncTask 正在执行并且ProgressDialog 正在显示时按主页按钮。

一旦 Activity 被隐藏,Android 操作系统就会销毁它。当onPostExecute 被调用时,Activity 将处于"finishing" 状态并且ProgressDialog 将不会附加到Activity

如何解决:

  1. onPostExecute 方法中检查活动状态。
  2. 关闭onDestroy 方法中的ProgressDialog。否则,将抛出android.view.WindowLeaked 异常。此异常通常来自活动结束时仍处于活动状态的对话框。

试试这个固定代码:

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

【讨论】:

  • 很好的答案!顺便说一句,isShowing() 是不必要的,因为如果 isShowing() == false,dismiss() 将什么也不做。 Source Code
  • 在所有 API 上使用 isDestroyed() 而不是 isFinishing() 有什么好处?
  • @AlexanderAbakumov:据我了解,isFinishing() 不保证是true,如果活动被系统销毁,请参阅documentation on Activities
  • @MarkusPenguin:对。但是,在这种情况下,如果有人试图使用作者评论 // or call isFinishing() if min sdk version &lt; 17 中的建议,他会遇到同样的异常。因此,对于在 API 上运行的应用程序,我们需要一个与此答案不同的解决方案。
  • @AlexanderAbakumov 我遇到了同样的问题,但是 isFinishing 对我不起作用,它可以在 AsyncCallback 中保存对我的活动的 WeakReference,然后:myActivityWeakReference.get() != null &amp;&amp; !myActivityWeakReference.get().isFinishing()
【解决方案2】:

覆盖 onConfigurationChanged 并关闭进度对话框。 如果进度对话框以纵向创建并以横向关闭,则会抛出 View not attach to window manager 错误。

同时在onPause()、onBackPressed和onDestroy方法中停止进度条和停止异步任务。

if(asyncTaskObj !=null && asyncTaskObj.getStatus().equals(AsyncTask.Status.RUNNING)){

    asyncTaskObj.cancel(true);

}

【讨论】:

    【解决方案3】:

    在此处查看代码的工作方式:

    调用异步任务后,异步任务在后台运行。这是可取的。现在,如果您询问如何查看代码,此异步任务有一个附加到 Activity 的进度对话框:

    pDialog = new ProgressDialog(CLASS.this);
    

    您将 Class.this 作为上下文传递给参数。所以进度对话框仍然附加到活动中。

    现在考虑以下场景: 如果我们尝试使用 finish() 方法完成活动,而异步任务正在进行中,那么当活动不再存在时,您将尝试访问附加到活动的资源,即progress bar

    因此你得到:

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

    解决方案:

    1) 确保在活动完成之前关闭或取消对话框。

    2) 完成activity,只有在对话框关闭后,即异步任务结束。

    【讨论】:

    • 关于 #2:您无法完全控制何时完成 Activity; Android可能随时完成它。所以,#2 没有意义。
    【解决方案4】:

    问题可能是Activity 一直是finishedprogress of finishing

    添加一个检查 isFinishing ,只有当这是 false 时才关闭对话框

    if (!YourActivity.this.isFinishing() && pDialog != null) {
        pDialog.dismiss();
    }
    

    isFinishing : 检查此活动是否正在结束,要么是因为你在它上面调用了finish,要么是其他人要求它完成。

    【讨论】:

    • 通过检查异常仍然被抛出
    • 我的 ProgressDialog 在一个不是活动的类中,所以我无法使用 isFinishing 检查@Libin
    【解决方案5】:
    @Override
    public void onPause() {
        super.onPause();
    
        if(pDialog != null)
            pDialog .dismiss();
        pDialog = null;
    }
    

    请参考this

    【讨论】:

      【解决方案6】:

      这个问题是因为您的活动在调用解除函数之前完成。处理异常并检查您的 ADB 日志以了解确切原因。

      /**
           * After completing background task Dismiss the progress dialog
           * **/
          protected void onPostExecute(String file_url) {
          try {
               if (pDialog!=null) {
                  pDialog.dismiss();   //This is line 624!    
               }
          } catch (Exception e) {
              // do nothing
          }
           something(note);
      }
      

      【讨论】:

        【解决方案7】:

        基于@erakitin 的回答,但也兼容 Android 版本 Activity.isDestroyed() 仅从 API 级别 17 开始受支持,因此如果您像我一样针对较旧的 API 级别,您将拥有自己检查。之后没有出现View not attached to window manager 异常。

        示例代码

        public class MainActivity extends Activity {
            private TestAsyncTask mAsyncTask;
            private ProgressDialog mProgressDialog;
            private boolean mIsDestroyed;
        
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                if (condition) {
                    mAsyncTask = new TestAsyncTask();
                    mAsyncTask.execute();
                }
            }
        
            @Override
            protected void onResume() {
                super.onResume();
        
                if (mAsyncTask != null && mAsyncTask.getStatus() != AsyncTask.Status.FINISHED) {
                    Toast.makeText(this, "Still loading", Toast.LENGTH_LONG).show();
                    return;
                }
            }
        
            @Override
            protected void onDestroy() {
                super.onDestroy();
                mIsDestroyed = true;
        
                if (mProgressDialog != null && mProgressDialog.isShowing()) {
                    mProgressDialog.dismiss();
                }
            }
        
            public class TestAsyncTask extends AsyncTask<Void, Void, AsyncResult> {    
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
                    mProgressDialog = ProgressDialog.show(MainActivity.this, "Please wait", "doing stuff..");
                }
        
                @Override
                protected AsyncResult doInBackground(Void... arg0) {
                    // Do long running background stuff
                    return null;
                }
        
                @Override
                protected void onPostExecute(AsyncResult result) {
                    // Use MainActivity.this.isDestroyed() when targeting API level 17 or higher
                    if (mIsDestroyed)// Activity not there anymore
                        return;
        
                    mProgressDialog.dismiss();
                    // Handle rest onPostExecute
                }
            }
        }
        

        【讨论】:

          【解决方案8】:

          覆盖 Activity 的 onDestroy 并关闭您的 Dialog 并将其设为 null

          protected void onDestroy ()
              {
                  if(mProgressDialog != null)
                      if(mProgressDialog.isShowing())
                          mProgressDialog.dismiss();
                  mProgressDialog= null;
              }
          

          【讨论】:

            【解决方案9】:

            我有办法重现此异常。

            我使用 2 AsyncTask。一个做长任务,另一个做短任务。短任务完成后,拨打finish()。当长任务完成并调用Dialog.dismiss()时,它会崩溃。

            这是我的示例代码:

            public class MainActivity extends Activity {
                private static final String TAG = "MainActivity";
                private ProgressDialog mProgressDialog;
            
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    Log.d(TAG, "onCreate");
            
                    new AsyncTask<Void, Void, Void>(){
                        @Override
                        protected void onPreExecute() {
                            mProgressDialog = ProgressDialog.show(MainActivity.this, "", "plz wait...", true);
                        }
            
                        @Override
                        protected Void doInBackground(Void... nothing) {
                            try {
                                Log.d(TAG, "long thread doInBackground");
                                Thread.sleep(20000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
            
                            return null;
                        }
            
                        @Override
                        protected void onPostExecute(Void result) {
                            Log.d(TAG, "long thread onPostExecute");
                            if (mProgressDialog != null && mProgressDialog.isShowing()) {
                                mProgressDialog.dismiss();
                                mProgressDialog = null;
                            }
                            Log.d(TAG, "long thread onPostExecute call dismiss");
                        }
                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            
                    new AsyncTask<Void, Void, Void>(){
                        @Override
                        protected Void doInBackground(Void... params) {
                            try {
                                Log.d(TAG, "short thread doInBackground");
                                Thread.sleep(5000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            return null;
                        }
            
                        @Override
                        protected void onPostExecute(Void aVoid) {
                            super.onPostExecute(aVoid);
                            Log.d(TAG, "short thread onPostExecute");
                            finish();
                            Log.d(TAG, "short thread onPostExecute call finish");
                        }
                    }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
                }
            
                @Override
                protected void onDestroy() {
                    super.onDestroy();
                    Log.d(TAG, "onDestroy");
                }
            }
            

            您可以尝试一下,找出解决此问题的最佳方法。 根据我的研究,至少有 4 种方法可以解决它:

            1. @erakitin 的回答:致电isFinishing() 检查活动状态
            2. @Kapé 的回答:设置标志以检查活动的状态
            3. 使用 try/catch 来处理。
            4. onDestroy() 中致电AsyncTask.cancel(false)。它将阻止异步任务执行onPostExecute(),而是执行onCancelled()
              注意:即使您在 Android 2.X.X 等旧版 Android 操作系统上调用 AsyncTask.cancel(false)onPostExecute() 仍会执行。

            您可以选择最适合您的。

            【讨论】:

              【解决方案10】:

              最佳解决方案。检查第一个上下文是活动上下文还是应用程序上下文 如果活动上下文则只检查活动是否完成然后调用dialog.show()dialog.dismiss();

              查看下面的示例代码...希望对您有所帮助!

              显示对话框

              if (context instanceof Activity) {
                 if (!((Activity) context).isFinishing())
                   dialog.show();
              }
              

              关闭对话框

              if (context instanceof Activity) {
                     if (!((Activity) context).isFinishing())
                       dialog.dismiss();
                  }
              

              如果您想添加更多检查,请使用&amp;&amp; 条件添加dialog.isShowing()dialog !-null

              【讨论】:

                【解决方案11】:

                对于在Fragment 中创建的Dialog,我使用以下代码:

                ProgressDialog myDialog = new ProgressDialog(getActivity());
                myDialog.setOwnerActivity(getActivity());
                ...
                Activity activity = myDialog.getOwnerActivity();
                if( activity!=null && !activity.isFinishing()) {
                    myDialog.dismiss();
                }
                

                我使用这种模式来处理Fragment 可能与Activity 分离的情况。

                【讨论】:

                  【解决方案12】:

                  可能是您全局初始化 pDialog,然后将其删除并在本地初始化您的视图或对话框。我有同样的问题,我已经这样做了,我的问题得到了解决。希望它对你有用。

                  【讨论】:

                    【解决方案13】:

                    首先,crash的原因是decorView的index为-1,从Android源码中可以知道,有代码sn-p:

                    类:android.view.WindowManagerGlobal

                    文件:WindowManagerGlobal.java

                    private int findViewLocked(View view, boolean required) {
                            final int index = mViews.indexOf(view);
                    //here, view is decorView,comment by OF
                            if (required && index < 0) {
                                throw new IllegalArgumentException("View=" + view + " not attached to window manager");
                            }
                            return index;
                        }
                    

                    所以得到follow resolution,判断decorView的index,如果大于0则继续或者直接返回放弃dismiss,代码如下:

                    try {
                                Class<?> windowMgrGloable = Class.forName("android.view.WindowManagerGlobal");
                                try {
                                    Method mtdGetIntance = windowMgrGloable.getDeclaredMethod("getInstance");
                                    mtdGetIntance.setAccessible(true);
                                    try {
                                        Object windownGlobal = mtdGetIntance.invoke(null,null);
                                        try {
                                            Field mViewField = windowMgrGloable.getDeclaredField("mViews");
                                            mViewField.setAccessible(true);
                                            ArrayList<View> mViews = (ArrayList<View>) mViewField.get(windownGlobal);
                                            int decorViewIndex = mViews.indexOf(pd.getWindow().getDecorView());
                                            Log.i(TAG,"check index:"+decorViewIndex);
                                            if (decorViewIndex < 0) {
                                                return;
                                            }
                                        } catch (NoSuchFieldException e) {
                                            e.printStackTrace();
                                        }
                                    } catch (IllegalAccessException e) {
                                        e.printStackTrace();
                                    } catch (InvocationTargetException e) {
                                        e.printStackTrace();
                                    }
                                } catch (NoSuchMethodException e) {
                                    e.printStackTrace();
                                }
                            } catch (ClassNotFoundException e) {
                                e.printStackTrace();
                            }
                            if (pd.isShowing()) {
                                pd.dismiss();
                            }
                    

                    【讨论】:

                    • 这是一个危险的 hack,依赖于许多内部实现细节。
                    • 这是一种保护,而不是黑客攻击。
                    • 看到有人否认这是黑客行为,这实际上是相当令人震惊的。
                    • 4年后,我搜索了我的答案,谢谢,^_^!!!
                    【解决方案14】:

                    我们还关闭了关于onPause 方法或onDestroy 方法的对话框

                    @Override
                    protected void onPause() {
                        super.onPause();
                        dialog.dismiss();
                    }
                    
                    @Override
                    protected void onDestroy() {
                        super.onDestroy();
                        dialog.dismiss();
                    }
                    

                    【讨论】:

                      【解决方案15】:

                      像这样覆盖dismiss() 方法:

                      @Override
                      public void dismiss() {
                          Window window = getWindow();
                          if (window == null) {
                              return;
                          }
                          View decor = window.getDecorView();
                          if (decor != null && decor.getParent() != null) {
                              super.dismiss();
                          }
                      }
                      

                      要重现该问题,只需在关闭对话框之前完成活动即可。

                      【讨论】:

                        【解决方案16】:

                        您可以尝试检查this解决方案或尝试以下解决方案。

                        if (pDialog instanceof Activity && !((Activity) mContext).isFinishing())
                                pDialog.show();
                        

                        【讨论】:

                          【解决方案17】:

                          检查所有者活动是否仍然存在:

                          if (dialog.getOwnerActivity() ==null || dialog.getOwnerActivity().isFinishing()) {
                              dialog.dismiss();
                          }
                          

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 1970-01-01
                            • 2011-03-23
                            • 2012-06-09
                            • 1970-01-01
                            • 2013-11-01
                            • 2011-01-14
                            相关资源
                            最近更新 更多