安卓:无法添加头的ListView(LinearLayout中不能在AbsListView投) [英] Android: Can't add header to ListView (LinearLayout can't be cast in AbsListView)

查看:305
本文介绍了安卓:无法添加头的ListView(LinearLayout中不能在AbsListView投)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加页眉用的ImageView我的ListView。我把ImageView的在XML的布局和在活动,我膨胀的布局,并把它添加到ListView头,但是当我运行应用程序崩溃说java.lang.ClassCastException:android.widget.LinearLayout $的LayoutParams不能转换为android.widget.AbsListView $的LayoutParams

我试图改变的LinearLayout到其他布局,但仍然收到此错误信息。有什么想法?

list_header_banner.xml code:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直>
< ImageView的
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / publicidadeView
    机器人:layout_marginTop =15dp
    机器人:layout_marginBottom =15dp
    机器人:layout_marginLeft =15dp
    机器人:layout_marginRight =15dp
    机器人:scaleType =fitXY
    机器人:可点击=真
    />
< / LinearLayout中>

活动充气code样品

 的ListView列表=(ListView控件)findViewById(R.id.lvResults);
查看headerView = View.inflate(这一点,R.layout.list_header_banner,NULL);
list.addHeaderView(headerView);尝试{
     ImageView的publicidadeView =(ImageView的)headerView.findViewById(R.id.publicidadeView);
}赶上(NullPointerException异常五){
}

活动的布局code样品

 < ListView控件
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:ID =@ + ID / lvResults
    机器人:layout_gravity =CENTER_HORIZONTAL |顶
    机器人:分=@绘制/除法
    />


解决方案

我只是用这个片段尝试,它为我工作。

  LayoutInflater吹气=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
的LinearLayout headerLayout =(的LinearLayout)inflater.inflate(R.layout.list_header_banner,NULL);
listView.addHeaderView(headerLayout);

I am trying to add a header with a ImageView to my ListView. I put the ImageView in an XML layout and in the Activity I am inflating the layout and add it to the ListView header, but when I run the app it crashes saying "java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams".

I tried changing the LinearLayout to other layouts, but still receiving this error message. Any thoughts?

list_header_banner.xml code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/publicidadeView"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    android:layout_marginLeft="15dp"
    android:layout_marginRight="15dp"
    android:scaleType="fitXY"
    android:clickable="true"
    />
</LinearLayout>

Activity inflate code sample

ListView list = (ListView)findViewById(R.id.lvResults);
View headerView = View.inflate(this, R.layout.list_header_banner, null);
list.addHeaderView(headerView);

try {
     ImageView publicidadeView = (ImageView) headerView.findViewById(R.id.publicidadeView);
} catch (NullPointerException e) {
}

Activity layout code sample

<ListView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/lvResults"
    android:layout_gravity="center_horizontal|top"
    android:divider="@drawable/divider"
    /> 

解决方案

I just tried using this snippet and it works for me.

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout headerLayout = (LinearLayout) inflater.inflate(R.layout.list_header_banner, null);
listView.addHeaderView(headerLayout);

这篇关于安卓:无法添加头的ListView(LinearLayout中不能在AbsListView投)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆