java.lang.ClassCastException while adding HeaderView to List

153 views Asked by At

I am getting following exception while adding header to listview on the line channellist.addHeaderView(header);-

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

I have tried both-

View header = View.inflate(getActivity(), R.layout.headerchannel, null);

and

View header = inflater.inflate(R.layout.headerchannel, null,false);

but same exception I am getting. My HomeActivity do have a framelayout and it is required for my project.

3

There are 3 answers

1
Ellie Zou On

You should use

View header = inflater.inflate(R.layout.headerchannel, null);
2
Dmitry Ognev On

if u see:

java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

u must do this:

header.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));

before channellist.addHeaderView(header);

2
Moinkhan On

I think in your header your root element is RelativeLayout. Which is ViewGroup not View. So Inflate it in a ViewGroup.

That's the reason it gives you ClassCastException. You are trying to convert ViewGroup intp View.

So first of all check that your layout is ViewGroup or View. If it's viewgroup you have to do this ..

ViewGroup header = LayoutInflater.from(getActivity()).inflate(R.layout.headerchannel, null);

Or it's just a view then..

View header = LayoutInflater.from(getActivity()).inflate(R.layout.headerchannel, null);