ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams错误提示
2015-08-25 10:38
375 查看
今天写一个ListView,用的是指定义的布局,这个ListView的headerView也是自定义布局,运行报错:
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
cannot be cast to android.widget.AbsListView$LayoutParams
网上查询,然后通过同事帮助才明白:如果你要将一个view添加到另一个布局中,你必须设定该View的布局参数为其父类所使用的布局参数类型。即要在代码中动态改变某组件的高度,其布局参数类型应该是其父类所使用的布局参数类型。
参考我的代码,因为ListView的headerView的LayoutParams参数没有改为和ListView一致;
改为
ListView.LayoutParams LP=new ListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
后正常显示。
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
cannot be cast to android.widget.AbsListView$LayoutParams
网上查询,然后通过同事帮助才明白:如果你要将一个view添加到另一个布局中,你必须设定该View的布局参数为其父类所使用的布局参数类型。即要在代码中动态改变某组件的高度,其布局参数类型应该是其父类所使用的布局参数类型。
参考我的代码,因为ListView的headerView的LayoutParams参数没有改为和ListView一致;
改为
ListView.LayoutParams LP=new ListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
后正常显示。
相关文章推荐
- Android URI简介
- Android Studio调试功能使用总结【转】
- Android Fragment 详细翻译
- Android画布更新过程OnDraw调用过程
- ViewPager实现引导页
- 总结系列-Android的文件系统
- Android自动化测试框架对比
- android user如何打开一个版本号root才干
- AndroidManifest中activity属性参数设置大全
- Android studio百度地图定位和地图开发
- 【转】Android 消息机制
- Android性能优化典范(三)
- Android自定义对话框(Dialog)位置,大小
- Android中的动画,属性动画
- Android 安全机制
- Android中View的绘制过程 onMeasure和onLayout()方法
- android下隐藏标题栏
- Android Studio快捷键
- Android-加载大图片
- Android控件使用—Fragment的基础知识