Android M ClassCastException: FrameLayout$LayoutParams cannot be cast to WindowManager$LayoutParams

做~自己de王妃 提交于 2019-11-29 00:42:16

Just add one more .getParent() to access the container.

 if (android.os.Build.VERSION.SDK_INT > 22) {
            container = (View) pwindow.getContentView().getParent().getParent();
        }else{
            container = (View) pwindow.getContentView().getParent();
        }

There is a class cast exception. You're casting a ViewGroup.LayoutParams to a WindowManager.LayoutParams. If the actual object returned is an instance of a different child of ViewGroup.LayoutParams (for example, FrameLayout.LayoutParams) then the cast is illegal. In this case the view called parent isn't directly in a window, its inside a FrameLayout. So calling getLayoutParams returns a FrameLayout.LayoutParams, not a WindowsManager.LayoutParams.

If it is working in 22 and not 23, its quite possible that they changed how popups are done in 23. Relying on the parent popup being directly inside of a window was never a safe assumption, your code always had the risk of being broken by an OS update.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!