When switching between addHeader and removeHeader, the program crashes. response. addheader
A problem occurred while working on the project. I will describe the problem here: when you are not logged on, a header layout will be displayed on the friend's dynamic page (the entire dynamic list uses listview) for prompting you to log on; after a user logs in, the header layout disappears.
Then, a crash occurred while I was debugging! I am debugging this way: first, go to the page and click the login button to log on. Log on to the logout account page to log off the Logon account. Then return to this friend dynamic page. Then, the problem arises! Report: "Cannot add header view to list -- setAdapter has already been called." How can this problem be solved? First, let's take a look at how the setadapter source code of listview runs.
public void addHeaderView(View v, Object data, boolean isSelectable) { if (mAdapter != null && ! (mAdapter instanceof HeaderViewListAdapter)) { throw new IllegalStateException( "Cannot add header view to list -- setAdapter has already been called."); } FixedViewInfo info = new FixedViewInfo(); info.view = v; info.data = data; info.isSelectable = isSelectable; mHeaderViewInfos.add(info); // in the case of re-adding a header view, or adding one later on, // we need to notify the observer if (mAdapter != null && mDataSetObserver != null) { mDataSetObserver.onChanged(); }}We can understand the above Code. When the listview has been setadapter (or previously setadapter, the second operation addheader), and then the listview addheader. According to the source code above, mAdapter! = Null already exists, so an error is reported! All, We can reset the mAdapter object. Before listview calls the addheader method, we should first use "listView. setAdapter (null);" and then add a header to the listview. My code is written as follows:
<Pre name = "code" class = "java"> @ Overridepublic void onStart () {super. onStart (); listView. setAdapter (null); // resolves the conflicting bugif (TextUtils. isEmpty (SharedPreferenceMemoryUtil. getToken (getActivity () {if (! HasHeader) {listView. addHeaderView (headView);} hasHeader = true;} else {if (hasHeader) {listView. removeHeaderView (headView); hasHeader = false ;}} requestMethod (); initBroadCastMethod ();}
Then, link to a friend who has similar problems!