本文介绍了ListFragment添加headerView给java.lang.IllegalStateException:alled的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经由一个tabhost maganed 2 ListFragments。 Firt时间我选项卡之间切换,一切都是正常的。该headerView正确添加。我第二次片段之间切换,我得到这样的execption:

I ve two ListFragments maganed by a tabhost. Firt time I switch between tab all is alright. The headerView is correctly added. The second time I switch between fragment I get this execption:

10-05 15:15:01.585: ERROR/AndroidRuntime(23263): java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
10-05 15:15:01.585: ERROR/AndroidRuntime(23263):     at android.widget.ListView.addHeaderView(ListView.java:261)
10-05 15:15:01.585: ERROR/AndroidRuntime(23263):     at android.widget.ListView.addHeaderView(ListView.java:284)
10-05 15:15:01.585: ERROR/AndroidRuntime(23263):     at it.chiesacattolica.archive.ArchiveFragment.onActivityCreated(ArchiveFragment.java:199)
10-05 15:15:01.585: ERROR/AndroidRuntime(23263):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:858)
10-05 15:15:01.585: ERROR/AndroidRuntime(23263):     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1183)

和应用程序崩溃。在code我用添加页眉观点如下:

and the app crashes. The code I use to add the header view follows:

@Override
public void onActivityCreated(Bundle arg0) {
    super.onActivityCreated(arg0);
    getListView().setOnScrollListener(this);
    getListView().addHeaderView(searchBox);
    setListAdapter(mAdapter);
}

什么是错的?在此先感谢

what's wrong ? thanks in advance

推荐答案

在Android开发者引用一个短暂的样子之后,我猜你已经设置适配器后,你不能这样做的另一个调用addHeaderView。
如果有这样的事情,你可以卸载适配器,添加新的HeaderView并重新加载适配器。如果不是这样,所有的HeaderViews应该调用setAdapter方法​​之前加入。

After a fleeting look at the android developers references, I'm guessing you can't do another call to addHeaderView after you've set the adapter.If there is such a thing, you can unload the adapter, add the new HeaderView and re-load the adapter. If not, all HeaderViews should be added before calling the setAdapter method.

看ListView.addHeaderView

这篇关于ListFragment添加headerView给java.lang.IllegalStateException:alled的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 00:34