When switching between addHeader and removeHeader, the program crashes. response. addheader

Source: Internet
Author: User

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! 



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.