android View not attached to window manager

后端 未结 13 2334
臣服心动
臣服心动 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: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

提交回复
热议问题