---
title: "ClassCastException"
author: "wcgdonot"
date: "2014-11-05T17:35:36Z"
tags: []
categories: ["移动开发"]
slug: "classcastexception"
draft: false
lastmod: "2014-11-05T17:35:36Z"
id: 2152708
url: "https://vvlavida.iteye.com/blog/2152708"
---

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);