ListFragment 添加 headerView 给出 java.lang.IllegalStateException:alled

发布于 2024-12-07 22:37:51 字数 1223 浏览 2 评论 0原文

我有两个由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

很酷又爱笑 2024-12-14 22:37:51

快速浏览一下 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

暖阳 2024-12-14 22:37:51

我认为这可能是您问题的答案,我不能肯定地说,因为您没有提供太多代码:

在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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文