当尝试将HeaderView添加到listView android时,ClassCastException [英] ClassCastException when trying to Add HeaderView to listView android

查看:128
本文介绍了当尝试将HeaderView添加到listView android时,ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是代码

 

code> mainLayout是放置ListView的SwipeLayout的父项
ViewGroup headerView =(ViewGroup)getLayoutInflater()。inflate(R.layout.profile_header,(RelativeLayout)findViewById(R.id.mainLayout))假);
listView.addHeaderView(headerView,null,false);
adapter = new TrafficTweatsAdapter(this,tweatsList);
listView.setAdapter(adapter);

我收到此错误

 导致:java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams不能被转换为android.widget.AbsListView $ LayoutParams 
在android.widget.ListView.clearRecycledState(ListView。 java:513)
在android.widget.ListView.resetList(ListView.java:499)
在android.widget.ListView.setAdapter(ListView.java:442)
在com.app .custom.EndlessListView.setAdapter(EndlessListView.java:45)

如果我把null替换为bgLayout或返回true,它给我错误

  java.lang.NullPointerException 
在android.widget.AbsListView.obtainView(AbsListView .java:2269)
at android.widget.ListView.makeAndAddView(ListView.java:1769)


解决方案

你错了,因为你以错误的方式膨胀布局。我们来看看文档


public View inflate(int resource,ViewGroup root,boolean attachToRoot)



root可选视图是生成的层次结构的父级(如果attachToRoot为true),或者只是一个对返回的层次结构的根提供一组LayoutParams值的对象(如果attachToRoot为false)。


所以,你将 RelativeLayout 指定为paretnt和 RelativeLayout.LayoutParams 正在从该布局中获取。之后,您尝试将该视图添加到 ListView ,它使用 AbsListView.LayoutParams 布局参数。



您应该可以通过将 ListView 指定为父代而增加页脚,而不是 mainLayout


I am trying to add HeaderView to my EndlessListView

Here is the code

mainLayout is the parent of SwipeLayout in which ListView is placed
ViewGroup headerView = (ViewGroup) getLayoutInflater().inflate(R.layout.profile_header, (RelativeLayout) findViewById(R.id.mainLayout), false);
        listView.addHeaderView(headerView, null, false);
        adapter = new  TrafficTweatsAdapter(this, tweatsList);
        listView.setAdapter(adapter);

I am getting this error

 Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
            at android.widget.ListView.clearRecycledState(ListView.java:513)
            at android.widget.ListView.resetList(ListView.java:499)
            at android.widget.ListView.setAdapter(ListView.java:442)
            at com.app.custom.EndlessListView.setAdapter(EndlessListView.java:45)

If i put null instead of bgLayout or return true, it gives me error

java.lang.NullPointerException
            at android.widget.AbsListView.obtainView(AbsListView.java:2269)
            at android.widget.ListView.makeAndAddView(ListView.java:1769)

解决方案

You are getting error cause you are inflating layout in wrong way. Let's take a look to documentation:

public View inflate (int resource, ViewGroup root, boolean attachToRoot)

root Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)

So, you are specifying RelativeLayout as paretnt and RelativeLayout.LayoutParams are taking from that layout. After that you are trying to add that view to ListView which useses AbsListView.LayoutParams layout params.

You should be able to add footer by inflating it with ListView specified as parent instead mainLayout

这篇关于当尝试将HeaderView添加到listView android时,ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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