简体   繁体   中英

addHeaderView on ListFragment = NullPointerException

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 !

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM