gpt4 book ai didi

安卓 M ClassCastException : FrameLayout$LayoutParams cannot be cast to WindowManager$LayoutParams

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:38 38 4
gpt4 key购买 nike

这段代码似乎适用于 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

最佳答案

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

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

关于安卓 M ClassCastException : FrameLayout$LayoutParams cannot be cast to WindowManager$LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36093254/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com