2

I have one issues in my application, I am setting header view to listview dynamically but am getting below error, I have two activities , A and B according my condition I am setting header view to listview when I am setting headerview in A activity it works fine but when A activity false my condition and go to B activity there my condition is true then come to A activity i need to add header view there I am getting Error................ I have tried to added onStart(), onResume() methods, but still am getting same error..... how to fix it

Java code

on Strat()
{


             if (mDrawerList.getHeaderViewsCount()<1) {
             TextView headerText = new TextView(mContext);

             headerText.setGravity(Gravity.CENTER);
             headerText.setTextColor(getResources().getColor(R.color.white_color));
             headerText.setPadding(20, 12, 20, 12);
             headerText.setTextSize(18);
             headerText.setText(mSessionManager.getUserName());
             mDrawerList.addHeaderView(headerText);
             }


         adapter = new NavDrawerListAdapter(mContext, navDrawerItems);
    mDrawerList.setDividerHeight(2);

    mDrawerList.setAdapter(adapter);
    adapter.notifyDataSetChanged();

    }

Error message

2-20 15:15:34.799: E/AndroidRuntime(13111): FATAL EXCEPTION: main
12-20 15:15:34.799: E/AndroidRuntime(13111): java.lang.RuntimeException: Unable to resume activity {com.examle.EventListActivity}: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
12-20 15:15:34.799: E/AndroidRuntime(13111):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2819)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at android.os.Looper.loop(Looper.java:137)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at android.app.ActivityThread.main(ActivityThread.java:5103)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at java.lang.reflect.Method.invokeNative(Native Method)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at java.lang.reflect.Method.invoke(Method.java:525)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at dalvik.system.NativeStart.main(Native Method)
12-20 15:15:34.799: E/AndroidRuntime(13111): Caused by: java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
12-20 15:15:34.799: E/AndroidRuntime(13111):    at android.widget.ListView.addHeaderView(ListView.java:258)
12-20 15:15:34.799: E/AndroidRuntime(13111):    at android.widget.ListView.addHeaderView(ListView.java:287)
venu
  • 2,971
  • 6
  • 40
  • 59
  • 3
    `Cannot add header view to list -- setAdapter has already been called.` That's mean .... well you have to add header first and then setAdapter to view ;] – Gooziec Dec 20 '13 at 09:58
  • befor setting adapter i am addeding header view – venu Dec 20 '13 at 10:07
  • It does not seem to be. You may be accessing a `ListView` that is already setup. Show us a little more part of the relevant code. – Rajesh Dec 20 '13 at 10:38
  • When you return from Activity B to A are you restarting the activity A?? if not then its the same instance you are facing and the listview present already has an adapter set – Rat-a-tat-a-tat Ratatouille Jan 16 '14 at 09:35
  • Try my answer here at http://stackoverflow.com/a/31181366/4489494 hope this will helpful to you.. – Kishan Soni Jul 02 '15 at 09:56
  • Try my answer here at http://stackoverflow.com/a/31181366/4489494 hope this will helpful to you.. – Kishan Soni Jul 02 '15 at 09:58

5 Answers5

8

Please take a look at the documentation of addHeaderView.

Give special attention to the note:

Note: When first introduced, this method could only be called before setting the adapter with setAdapter(ListAdapter). Starting with KITKAT, this method may be called at any time. If the ListView's adapter does not extend HeaderViewListAdapter, it will be wrapped with a supporting instance of WrapperListAdapter.

Rajesh
  • 15,724
  • 7
  • 46
  • 95
8

I found this simple workaround here http://code.neenbedankt.com/note-to-self-listfragment-and-header-views/:

    @Override
    public void onDestroyView() {
      super.onDestroyView();
      setListAdapter(null);
  }

Simply add this to the ListFragment class.
It works for me on 2.3.3 and 4.2.2

Einar H.
  • 575
  • 5
  • 5
4

How about:

    ListAdapter adapter = listView.getAdapter();
    listView.setAdapter(null);
    listView.addHeaderView(headerView);
    listView.setAdapter(adapter);

The only problem here, is that it restart your scrollbar position. But than there is no other way to add headerView after adapter been set.

Ilya Gazman
  • 31,250
  • 24
  • 137
  • 216
0

need to check whether the list has already set the adapter or not.

if (mDrawerList.getAdapter==null) {
    // add the header view here.this will work                      
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Ryada
  • 95
  • 1
  • 2
  • 14
-1
list.setAdapter(null);
list.addHeaderView(loading.getView());
David
  • 208,112
  • 36
  • 198
  • 279