视图未附加到窗口管理器崩溃

时间:2014-04-07 23:17:12

标签: android

我正在使用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)

代码段:

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>

我错过了什么来阻止这次崩溃?

15 个答案:

答案 0 :(得分:408)

如何重现错误:

  1. 在您的设备上启用此选项:Settings -> Developer Options -> Don't keep Activities
  2. 执行AsyncTask并显示ProgressDialog时,按主页按钮。
  3. Android操作系统会在隐藏后立即销毁活动。调用onPostExecute后,Activity将处于&#34;结束&#34; 状态,ProgressDialog将不会附加到Activity

    如何修复:

    1. 检查onPostExecute方法中的活动状态。
    2. 取消ProgressDialog方法中的onDestroy。否则,将抛出android.view.WindowLeaked异常。此异常通常来自活动结束时仍处于活动状态的对话框。
    3. 试试这个固定代码:

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

答案 1 :(得分:29)

问题可能是Activityfinishedprogress of finishing

添加支票isFinishing,仅在false

时关闭对话框
if (!YourActivity.this.isFinishing() && pDialog != null) {
    pDialog.dismiss();
}

isFinishing: 检查此活动是否正在完成,或者是因为您在其上调用了finish或其他人已请求完成此活动。

答案 2 :(得分:8)

了解守则如何运作:

调用异步任务后,异步任务在后台运行。这是可取的。现在这个Async任务有一个附加到Activity的进度对话框,如果你问如何,请参阅代码:

pDialog = new ProgressDialog(CLASS.this);

您将Class.this作为上下文传递给参数。因此,“进度”对话框仍附加到活动。

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

因此你得到:

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

解决方案:

1)确保在活动结束前取消或取消对话框。

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

答案 3 :(得分:7)

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

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

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

答案 4 :(得分:6)

基于@erakitin的答案,还兼容Android版本&lt; API等级17.可悲的是Activity.isDestroyed() 仅支持API等级17,因此,如果您像我一样定位较旧的API级别,则必须自行检查。在此之后,Haven没有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
        }
    }
}

答案 5 :(得分:4)

@Override
public void onPause() {
    super.onPause();

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

参考this

答案 6 :(得分:3)

覆盖onConfigurationChanged并关闭进度对话框。 如果以纵向创建进度对话框并在横向中关闭,则会抛出View未附加到窗口管理器错误。

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

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

    asyncTaskObj.cancel(true);

}

答案 7 :(得分:3)

覆盖活动的onDestroy并关闭对话框&amp;使其为空

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

答案 8 :(得分:3)

首先,崩溃的原因是decorView的索引是-1,我们可以从Android源代码中知道它,有代码片段:

  

类: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;
    }

所以我们得到以下解决方案,只需判断decorView的索引,如果它超过0然后继续或只是返回并放弃解雇,代码如下:

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

答案 9 :(得分:2)

像这样覆盖dismiss()方法:

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

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

答案 10 :(得分:1)

最佳解决方案。检查第一个上下文是活动上下文或应用程 如果活动上下文仅检查活动是否已完成,则调用dialog.show()dialog.dismiss();

  

请参阅下面的示例代码 ...希望它会有所帮助!

     

显示对话框

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

关闭对话框

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

如果您想添加更多支票,请使用dialog.isShowing()条件添加dialog !-null&&

答案 11 :(得分:0)

此问题是因为您的活动在调用dismiss函数之前完成。处理异常并检查您的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);
}

答案 12 :(得分:0)

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

答案 13 :(得分:0)

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

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

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

答案 14 :(得分:0)

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

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