Android M ClassCastException:无法将FrameLayout $ LayoutParams强制转换为WindowManager $ LayoutParams [英] Android M ClassCastException: FrameLayout$LayoutParams cannot be cast to WindowManager$LayoutParams
问题描述
这段代码似乎可以在android api 16-22中工作,但不能在api 23中工作.我只是试图显示一个带选项的弹出窗口,并使弹出窗口下方的背景变暗:
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)
出了什么问题?
据我所知,WindowManager.LayoutParams
扩展了ViewGroup.LayoutParams
,当我调用parent.getLayoutParams()
时,它返回了ViewGroup.LayoutParams
,因此不应有classCastException
.
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
.
推荐答案
只需再添加一个.getParent()即可访问容器.
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();
}
这篇关于Android M ClassCastException:无法将FrameLayout $ LayoutParams强制转换为WindowManager $ LayoutParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!