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
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