2

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)
1
  • Can you share TrafficTweatsAdapter getView method code Commented Mar 7, 2015 at 13:04

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

5 Comments

put listVIew instead of (RelativeLayout) findViewById(R.id.mainLayout)
i have placed listView inflate(R.layout.profile_header, listView, false); Now giving second NullPointerError FATAL EXCEPTION: main java.lang.NullPointerException at android.widget.AbsListView.obtainView(AbsListView.java:2269) at android.widget.ListView.makeAndAddView(ListView.java:1769) at android.widget.ListView.fillDown(ListView.java:672)
this probably mean that your adapter is not working properly. Show us your getView from adapter
@MuhammadUmar See similar Q at stackoverflow.com/questions/27015544/… Check return value in getView()
@andrew the adapter is fine. It is working without headerView only difference is the ArrayList size is 0 (Not null), but it shouldn't give any problem, if i remove headerview it works just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.