android View not attached to window manager

后端 未结 13 2244
臣服心动
臣服心动 2020-11-27 10:29

I am having some of the following exceptions:

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerImpl.findV         


        
相关标签:
13条回答
  • 2020-11-27 10:43

    After a fight with this issue, I finally end up with this workaround:

    /**
     * Dismiss {@link ProgressDialog} with check for nullability and SDK version
     *
     * @param dialog instance of {@link ProgressDialog} to dismiss
     */
    public void dismissProgressDialog(ProgressDialog dialog) {
        if (dialog != null && dialog.isShowing()) {
    
                //get the Context object that was used to great the dialog
                Context context = ((ContextWrapper) dialog.getContext()).getBaseContext();
    
                // if the Context used here was an activity AND it hasn't been finished or destroyed
                // then dismiss it
                if (context instanceof Activity) {
    
                    // Api >=17
                    if (!((Activity) context).isFinishing() {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                            if (!((Activity) context).isDestroyed()) {
                                dismissWithExceptionHandling(dialog);
                            }
                        } else { 
                            // Api < 17. Unfortunately cannot check for isDestroyed()
                            dismissWithExceptionHandling(dialog);
                        }
                    }
                } else
                    // if the Context used wasn't an Activity, then dismiss it too
                    dismissWithExceptionHandling(dialog);
            }
            dialog = null;
        }
    }
    
    /**
     * Dismiss {@link ProgressDialog} with try catch
     *
     * @param dialog instance of {@link ProgressDialog} to dismiss
     */
    public void dismissWithExceptionHandling(ProgressDialog dialog) {
        try {
            dialog.dismiss();
        } catch (final IllegalArgumentException e) {
            // Do nothing.
        } catch (final Exception e) {
            // Do nothing.
        } finally {
            dialog = null;
        }
    }
    

    Sometimes, good exception handling works well if there wasn't a better solution for this issue.

    0 讨论(0)
  • 2020-11-27 10:44

    My problem was solved by uhlocking the screen rotation on my android the app which was causing me a problem now works perfectly

    0 讨论(0)
  • 2020-11-27 10:46

    Above solution didn't work for me. So what I did is take ProgressDialog as globally and then add this to my activity

    @Override
        protected void onDestroy() {
            if (progressDialog != null && progressDialog.isShowing())
                progressDialog.dismiss();
            super.onDestroy();
        }
    

    so that in case if activity is destroyed then the ProgressDialog will also be destroy.

    0 讨论(0)
  • 2020-11-27 10:47

    according to the code of the windowManager (link here), this occurs when the view you are trying to update (which probably belongs to a dialog, but not necessary) is no longer attached to the real root of the windows.

    as others have suggested, you should check the status of the activity before performing special operations on your dialogs.

    here's the relavant code, which is the cause to the problem (copied from Android source code) :

    public void updateViewLayout(View view, ViewGroup.LayoutParams params) {
        if (!(params instanceof WindowManager.LayoutParams)) {
            throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
        }
    
        final WindowManager.LayoutParams wparams
                = (WindowManager.LayoutParams)params;
    
        view.setLayoutParams(wparams);
    
        synchronized (this) {
            int index = findViewLocked(view, true);
            ViewRootImpl root = mRoots[index];
            mParams[index] = wparams;
            root.setLayoutParams(wparams, false);
        }
    }
    
    private int findViewLocked(View view, boolean required) {
            synchronized (this) {
                final int count = mViews != null ? mViews.length : 0;
                for (int i=0; i<count; i++) {
                    if (mViews[i] == view) {
                        return i;
                    }
                }
                if (required) {
                    throw new IllegalArgumentException(
                            "View not attached to window manager");
                }
                return -1;
            }
        }
    
    0 讨论(0)
  • 2020-11-27 10:50

    I am using a custom static class which makes- shows and hides a dialog. this class is being used by other activities too not only one activiy. Now the problem you described also appeared to me and i have stayed overnight to find a solution..

    Finally i present you the solution!

    if you want to show or dismiss a dialog and you dont know which activity initiated the dialog in order to touch it then the following code is for you..

     static class CustomDialog{
    
         public static void initDialog(){
             ...
             //init code
             ...
         }
    
          public static void showDialog(){
             ...
             //init code for show dialog
             ...
         }
    
         /****This is your Dismiss dialog code :D*******/
         public static void dismissProgressDialog(Context context) {                
                //Can't touch other View of other Activiy..
                //http://stackoverflow.com/questions/23458162/dismiss-progress-dialog-in-another-activity-android
                if ( (progressdialog != null) && progressdialog.isShowing()) {
    
                    //is it the same context from the caller ?
                    Log.w("ProgressDIalog dismiss", "the dialog is from"+progressdialog.getContext());
    
                    Class caller_context= context.getClass();
                    Activity call_Act = (Activity)context;
                    Class progress_context= progressdialog.getContext().getClass();
    
                    Boolean is_act= ( (progressdialog.getContext()) instanceof  Activity )?true:false;
                    Boolean is_ctw= ( (progressdialog.getContext()) instanceof  ContextThemeWrapper )?true:false;
    
                    if (is_ctw) {
                        ContextThemeWrapper cthw=(ContextThemeWrapper) progressdialog.getContext();
                        Boolean is_same_acivity_with_Caller= ((Activity)(cthw).getBaseContext() ==  call_Act )?true:false;
    
                        if (is_same_acivity_with_Caller){
                            progressdialog.dismiss();
                            progressdialog = null;
                        }
                        else {
                            Log.e("ProgressDIalog dismiss", "the dialog is NOT from the same context! Can't touch.."+((Activity)(cthw).getBaseContext()).getClass());
                            progressdialog = null;
                        }
                    }
    
    
                }
            } 
    
     }
    
    0 讨论(0)
  • 2020-11-27 10:54

    Why not try catch, like this:

    protected void onPostExecute(Object result) {
            try {
                if ((mDialog != null) && mDialog.isShowing()) {
                    mDialog.dismiss();
                }
            } catch (Exception ex) {
                Log.e(TAG, ex.getMessage(), ex);
            }
        }
    
    0 讨论(0)
提交回复
热议问题