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

2

3

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 background below the popupwindow:

                WindowPopUp windowPopUp =
                        new WindowPopUp(mContext, mPlaces.get(position), position, fromSearch);
                windowPopUp.showAtLocation(v, Gravity.CENTER, 0, 0);
                View parent = (View) windowPopUp.getContentView().getParent();
                //dim the window in the background
                WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                WindowManager.LayoutParams p = (WindowManager.LayoutParams) parent.getLayoutParams();
                p.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;
                p.dimAmount = 0.4f;
                wm.updateViewLayout(parent, p);

Running this code results in an error:

                03-18 21:55:19.674 8814-8814/? E/AndroidRuntime: FATAL EXCEPTION: main
                                             Process: com.myapp, PID: 8814
                                             java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.view.WindowManager$LayoutParams
                                                 at com.bemyapp.adapter.OuterPlaceAdapter$5.onLongClick(OuterPlaceAdapter.java:400)
                                                 at android.view.View.performLongClick(View.java:5237)
                                                 at android.view.View$CheckForLongPress.run(View.java:21121)
                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:148)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

What has gone wrong?

As far as I know, WindowManager.LayoutParams extends ViewGroup.LayoutParams and when I call parent.getLayoutParams(), it returns a ViewGroup.LayoutParams so there should not be a classCastException.

Carrington answered 18/3, 2016 at 20:2 Comment(0)
A
10

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();
        }
Allout answered 6/4, 2016 at 11:29 Comment(0)
E
1

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.

Entomophagous answered 18/3, 2016 at 20:8 Comment(4)
Added an edit. Basically, you were depending on an implementation detail that they changed.Entomophagous
This is terrible news. I guess I would have to change all my popupwindows to DialogActivity. Popupwindows are not a good design idea if this is true.Carrington
Perhaps not for your usecase, although it is for others. Its also possible that you may be able to get what you want by walking up the view hierarchy until you hit a window (they may have just added a frame to handle multiple popups in a single window). But they may have just changed it so that both the main windows and popups are in a single frame in which case that won't work.Entomophagous
I am facing this similar issue in my popup window. Did you come up with a solution?Allout

© 2022 - 2024 — McMap. All rights reserved.