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)
6
  • 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 ;] Commented Dec 20, 2013 at 9:58
  • befor setting adapter i am addeding header view Commented Dec 20, 2013 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. Commented Dec 20, 2013 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 Commented Jan 16, 2014 at 9:35
  • Try my answer here at stackoverflow.com/a/31181366/4489494 hope this will helpful to you.. Commented Jul 2, 2015 at 9:56

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i have seen it but how to fix it
You can hide the view of the header using setVisibility method
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

Comments

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.

Comments

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                      
}

Comments

-1
list.setAdapter(null);
list.addHeaderView(loading.getView());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.