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

后端 未结 2 1485
时光取名叫无心
时光取名叫无心 2020-12-16 07:58

This piece of code works seems to work in android api 16 - 22 but does not work in api 23. I\'m simply trying to display a popupwindow with options in it and dim the backgro

相关标签:
2条回答
  • 2020-12-16 08:42

    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.

    0 讨论(0)
  • 2020-12-16 08:44

    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();
            }
    
    0 讨论(0)
提交回复
热议问题