问题描述
动态添加子view的时候,报crash,提示$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
完整错误堆栈
:12.946 11951-11951/com.xx E/CrashReport: sys default last handle start!
11-16 12:21:13.041 11951-11951/com.xx E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.xx, PID: 11951
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
at com.xx.TimeLineWithPicturesAdapter.setTimeLineHeight(TimeLineWithPicturesAdapter.java:223)
at com.xx.TimeLineWithPicturesAdapter.onBindViewHolder(TimeLineWithPicturesAdapter.java:205)
at com.xx.TimeLineWithPicturesAdapter.onBindViewHolder(TimeLineWithPicturesAdapter.java:48)
问题分析
View.getlayoutParams,返回的是父view的参数,父view 是什么,就得赋值给对应的父View.LayoutParams
问题解决
传递父view类型的LayoutParams