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


188

我正在使用ACRA报告应用程序崩溃。我收到一条View not attached to window manager错误消息,并认为我已经通过将pDialog.dismiss();if 包裹在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>

我想阻止该崩溃发生的原因是什么?


1
你有没有想过这个?我有同样的问题。似乎无法弄清楚。
tomjung 2014年

抱歉不行。我可能会开始赏金。您应该检查其他一些处理此类问题的线程,以防它们对您有所帮助。
Howli 2014年

AsyncTask是在内部声明Activity还是Fragment
erakitin

请发布函数“ doMoreStuff()”和“ something()”。
疯抢

该问题可能是由于主线程工作量过多,所以请尝试使用处理程序来显示progressdialog,并且如果(pDialog!= null)这行是不需要的,因为isShowing自身会检查对话框是否在进行中。
2014年

Answers:


446

如何重现该错误:

  1. 在设备上启用此选项:Settings -> Developer Options -> Don't keep Activities
  2. AsyncTask正在执行并ProgressDialog显示时,按Home键。

Android OS会在隐藏活动后立即销毁该活动。当onPostExecute被称为Activity将是“整理”状态,并且ProgressDialog将不附Activity

解决方法:

  1. 检查您的onPostExecute方法中的活动状态。
  2. 关闭ProgressDialogin onDestroy方法。否则,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);
        }
    }
}

8
好答案!顺便说一句,isShowing()是不必要的,因为如果isShowing()== false,dismiss()将不执行任何操作。源代码
Peter Zhao

3
为此目的isDestroyed()isFinishing()在所有API上使用over有什么好处?
亚历山大·阿巴库莫夫

@AlexanderAbakumov:据我所知isFinishing(),不能保证true该活动被系统破坏了,请参阅“ 活动文档
马库斯企鹅'18

1
@MarkusPenguin:对。但是,在这种情况下,如果尝试使用作者评论中的建议// or call isFinishing() if min sdk version < 17,他就会遇到同样的例外。所以,我们需要比这个答案不同的解决方案对API <17正在运行的应用
亚历山大Abakumov

@AlexanderAbakumov我遇到了同样的问题,但是isFinishing对我不起作用,它可以将WeakReference保存到AsyncCallback中我的活动中,然后:myActivityWeakReference.get() != null && !myActivityWeakReference.get().isFinishing()
rusito23 '18

34

问题可能是因为Activity已经finishedprogress of finishing

添加检查isFinishing,仅在以下情况下关闭对话框false

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

isFinishing: 检查此活动是否正在完成中,可能是因为您调用finish了它,还是其他人已要求完成它。


1
通过检查仍在抛出异常
Marcos Vasconcelos

我的ProgressDialog在不是活动的类中,因此我无法使用isFinishing检查@Libin
Maniraj,

10

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

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

当a Fragment可能与分离时,我使用这种模式来处理Activity


8

在这里查看代码的工作方式:

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

pDialog = new ProgressDialog(CLASS.this);

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

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

因此,您得到:

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

解决方案:

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

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


1
关于#2:您无法完全控制何时完成Activity; Android可能会随时完成它。因此,#2没有意义。
亚历山大·阿巴库莫夫

6

基于@erakitin答案,但也兼容<API级别17的Android版本。遗憾的是,仅从API级别17开始才支持Activity.isDestroyed(),因此,如果您像我一样定位较旧的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
        }
    }
}

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

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

请参考这个


3

覆盖onConfigurationChanged并关闭进度对话框。如果进度对话框是纵向创建的,而横向却是关闭的,则它将引发“视图未附加到窗口管理器”错误。

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

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

    asyncTaskObj.cancel(true);

}

3

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

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

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

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

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


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使用&&条件。


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

0

我有办法重现此异常。

我用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. 呼叫AsyncTask.cancel(false)onDestroy()。这将阻止asynctask执行,onPostExecute()而是onCancelled()改为执行。
    注意:onPostExecute()即使您AsyncTask.cancel(false)在较旧的Android操作系统(如Android 2.XX)上调用,仍会执行

您可以选择最适合自己的。


0

可能是您全局初始化了pDialog,然后将其删除并在本地初始化您的视图或对话框。希望它对你有用。


0

我们也取消了关于onPause方法或onDestroy方法的对话

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

@Override
protected void onDestroy() {
    super.onDestroy();
    dialog.dismiss();
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.