2

I encounter a problem when I try to add a header on the List of my ListFragment. I heard I had to add the header view before the setListAdapter (I followed this topic Best place to addHeaderView in ListFragment ) and yet, it still doesn't work.

Here is my code :

@Override
public void onActivityCreated (Bundle savedInstanceState)
{
    Log.i(General.TAG, "beginning of onActivityCreated");
    super.onActivityCreated(savedInstanceState);
    if (mheaderView != null)  this.getListView().addHeaderView(getView().findViewById(R.layout.header)); // <= mheaderView is not null
    ListView lv = getListView(); // <= lv is not null

    lv.setAdapter(adapter);
    Log.i(General.TAG, "end of onActivityCreated");
}

The stack trace generated on line "lv.setAdapter(adapter)" is this one :

   02-06 16:16:37.226: E/AndroidRuntime(25838): **java.lang.NullPointerException**
   02-06 16:16:37.226: E/AndroidRuntime(25838):     at android.widget.ListView.clearRecycledState(ListView.java:497)
   02-06 16:16:37.226: E/AndroidRuntime(25838):     at android.widget.ListView.resetList(ListView.java:483)
   02-06 16:16:37.226: E/AndroidRuntime(25838):     at android.widget.ListView.setAdapter(ListView.java:426)
   02-06 16:16:37.226: E/AndroidRuntime(25838):     at fr.pack.DetailFragment.onActivityCreated(DetailFragment.java:75)

But the adapter is not null, neither is the mHeaderView. Do you have any clue of the mistake I may have done ? Thanks in advance !

Community
  • 1
  • 1
Thordax
  • 1,673
  • 4
  • 28
  • 54
  • Possible duplicate of [Android ListView addHeaderView() nullPointerException for predefined Views defined in XML](http://stackoverflow.com/questions/7838921/android-listview-addheaderview-nullpointerexception-for-predefined-views-defin) – Amanda S Sep 21 '16 at 18:32

1 Answers1

0

I believe you problem lies on the addHeaderView, you are probably missing to inflate the view prior to put it on the method. I want no credits, I only faced the same problem and found your issue along with a similar issue that seems to resolve your problem as well:

Android adding footer to ListView addFooterView()?

Hope it helps

Community
  • 1
  • 1