Android M ClassCastException:FrameLayout $ LayoutParams无法强制转换为WindowManager $ LayoutParams

时间:2016-03-18 20:02:22

标签: android android-6.0-marshmallow layoutparams android-popupwindow

这段代码工作似乎在android api 16 - 22中工作但在api 23中不起作用。我只是试图在其中显示一个带有选项的弹出窗口并将背景调暗到弹出窗口下方:

                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);

运行此代码会导致错误:

                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)

出了什么问题?

据我所知,WindowManager.LayoutParams扩展了ViewGroup.LayoutParams,当我致电parent.getLayoutParams()时,它会返回ViewGroup.LayoutParams,因此不应该有classCastException

2 个答案:

答案 0 :(得分:10)

只需添加一个.getParent()即可访问容器。

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

答案 1 :(得分:1)

有一个类强制转换异常。您正在向ViewGroup.LayoutParams投射WindowManager.LayoutParams。如果返回的实际对象是ViewGroup.LayoutParams的不同子项的实例(例如,FrameLayout.LayoutParams),那么强制转换是非法的。在这种情况下,名为parent的视图不是直接在窗口中,而是在FrameLayout内。因此,调用getLayoutParams会返回FrameLayout.LayoutParams,而不是WindowsManager.LayoutParams

如果它在22而不是23中工作,很可能它们改变了弹出窗口在23中的完成方式。依赖于直接在窗口内的父弹出窗口从来都不是一个安全的假设,你的代码总是有风险被操作系统更新打破。