简体   繁体   English

ListFragment上的addHeaderView = NullPointerException

[英]addHeaderView on ListFragment = NullPointerException

I encounter a problem when I try to add a header on the List of my ListFragment. 尝试在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. 我听说我必须在setListAdapter之前添加标题视图(我遵循了ListFragment中addHeaderView的最佳位置这个主题),但是它仍然无法正常工作。

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 : 这是在"lv.setAdapter(adapter)"行上生成的堆栈跟踪:

   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. 但是适配器不为null,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. 我相信您的问题出在addHeaderView上,您可能缺少将视图放到方法上之前将其膨胀的想法。 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()? Android将页脚添加到ListView addFooterView()吗?

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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