classcastexception
关于一个奇怪的classcastexception:Android.widget.relativelayout$layoutparams cannot be cast to android.support.v4.widget.drawerLayout$LayoutParams
今天在写自定义相机的时候发现一个莫名其妙的异常情况:android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
具体Logcat打印的异常信息如下:可以看出没有报出具体的位置。
05-08 17:35:39.817 26736-26736/? E/Androidruntime: FATAL EXCEPTION: main
Process: com.example.lenovo.raispaceandroid, PID: 26736
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
at android.support.v4.widget.DrawerLayout.onmeasure(DrawerLayout.java:1016)
at android.view.View.measure(View.java:19886)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.contentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:19886)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19886)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19886)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19886)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6085)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:692)
at android.view.View.measure(View.java:19886)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2328)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1419)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1672)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1307)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6422)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:876)
at android.view.Choreographer.doCallbacks(Choreographer.java:688)
at android.view.Choreographer.doFrame(Choreographer.java:623)
at android.view.Choreographer$FramedisplayEventReceiver.run(Choreographer.java:862)
at android.os.handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchmessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.APP.activitythread.main(ActivityThread.java:6389)
at java.lang.reflect.method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:791)
搜了很多文章之后,仔细分析之后发现是因为我添加的自定义相机类Camerasurfaceview中有这么一段:
// 根据选出的PictureSize重新设置SurfaceView大小
float w = picSize.width;
float h = picSize.height;
parameters.setPictureSize(picSize.width, picSize.height); //设置照片分辨率大小
this.setLayoutParams(new RelativeLayout.LayoutParams((int) (height * (h / w)), height));
错误正式因为正是这一句:
this.setLayoutParams(new RelativeLayout.LayoutParams((int) (height * (h / w)), height))
。
因为我的Activity的根布局是DrawerLayout。
所以改为:
this.setLayoutParams(new DrawerLayout.LayoutParams((int) (height * (h / w)), height))
即可。
相关阅读
BeanUtils、PropertiesUtils、BeanCopier copyPropert
复制属性值,如果复制的实体对象中有list,在使用list的时候,如果list中的泛型是不一样的,就会出现类转化失败 java.lang.ClassCastE
ClassCastException异常的出现原因和解决方法
ClassCastException是JVM在检测到两个类型间转换不兼容时引发的运行时异常。此类错误通常会终止用户请求。在执行任何子系统的应