尝试将页脚添加到ListView时ClassCastException

时间:2014-06-30 08:10:15

标签: android listview android-listview classcastexception

我正在尝试向ListView添加页脚,并且我收到以下错误:

06-30 03:57:27.545: E/AndroidRuntime(10112): java.lang.ClassCastException: 
android.view.ViewGroup$LayoutParams cannot be cast to 
android.widget.AbsListView$LayoutParams

这是我的ListView的页脚(listview_footer.xml):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="7dip"
    android:paddingBottom="7dip"
    android:orientation="horizontal"
    android:gravity="center">

    <ImageButton
        android:id="@+id/addButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="12dp"
        android:src="@drawable/add_button" />

</LinearLayout>

以下是我用来将页脚添加到ListView的代码:

View footerView = inflater.inflate(R.layout.listview_footer, null, false);  

addButton = (ImageButton) footerView.findViewById(R.id.addButton);

listView.addFooterView(footerView);

在这一行:

View footerView = inflater.inflate(R.layout.listview_footer, null, false);

&#34; null&#34;在Eclipse中,参数显示为橙色线,当我将鼠标悬停在它上面时,它会显示:

Avoid passing null as the view root (needed to resolve layout 
parameters on the inflated layout's root element)

在这种情况下,我不确定我应该指定什么作为第二个参数(ViewGroup root)。

2 个答案:

答案 0 :(得分:2)

我认为你必须更换

View footerView = inflater.inflate(R.layout.listview_footer, null, false);

View footerView = inflater.inflate(R.layout.listview_footer, listview, false);

这样,inflater会给它正确的布局框架

答案 1 :(得分:-1)

在导入中使用android.widget.AbsListView.LayoutParams instead of the android.widget.LinearLayout.LayoutParams

ClassCastException栈跟踪非常具有描述性......