ListFragment 添加 headerView 给出 java.lang.IllegalStateException:alled
我有两个由 tabhost 管理的 ListFragment。我第一次在选项卡之间切换一切都很好。 headerView 已正确添加。第二次我在片段之间切换时,我得到了这个执行:
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)
并且应用程序崩溃了。我用来添加标题视图的代码如下:
@Override
public void onActivityCreated(Bundle arg0) {
super.onActivityCreated(arg0);
getListView().setOnScrollListener(this);
getListView().addHeaderView(searchBox);
setListAdapter(mAdapter);
}
出了什么问题?提前致谢
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)
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
快速浏览一下 android 开发人员参考资料后,我猜您在设置适配器后无法再次调用 addHeaderView 。
如果出现这种情况,您可以卸载适配器,添加新的 HeaderView 并重新加载适配器。如果没有,则应在调用 setAdapter 方法之前添加所有 HeaderView。
请参阅 ListView.addHeaderView
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.
see ListView.addHeaderView
我认为这可能是您问题的答案,我不能肯定地说,因为您没有提供太多代码:
在ListFragment中addHeaderView的最佳位置
I think this might be the answer to your question, I can't say for sure since you haven't provided very much code:
Best place to addHeaderView in ListFragment