0

I am trying to enable the Fast Scroll in ExpandableListView, and I've tried the workaround solution from this link: android fastScroll only covers part of the list

The problem is that it gives me the NullPointerException error whenever I expand enough groups to trigger the fast scroll bar. I've tried the following code:

MainActivity:
expandblelist = (ExpandableListView) findViewById(R.id.mexpandablelistview);
Adapter = new Adapter(this, expandblelist, category_array);
expandblelist.setAdapter(Adapter);
expandblelist.setFastScrollEnabled(true);

Adapter:
public class Adapter extends BaseExpandableListAdapter 
                                implements SectionIndexer, AbsListView.OnScrollListener {
    private Context mContext;
    private ExpandableListView mExpandableListView;
    private List<Category> mGroupCollection;
    private int[] groupStatus;
        private boolean manualScroll;
        private String[] groups;

    public Adapter(Context context, ExpandableListView pExpandableListView, List<Category> pGroupCollection) {
        mContext = context;
        mGroupCollection = pGroupCollection;
        mExpandableListView = pExpandableListView;
        groupStatus = new int[mGroupCollection.size()];
        setListEvent(); 
        this.mExpandableListView.setOnScrollListener(this);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        this.manualScroll = scrollState == SCROLL_STATE_TOUCH_SCROLL;
    }

    @Override
    public void onScroll(AbsListView view, 
                         int firstVisibleItem, 
                         int visibleItemCount, 
                         int totalItemCount) {}

    @Override
    public int getPositionForSection(int section) {
        if (manualScroll) {
            return section;
        } else {            
            return mExpandableListView.getFlatListPosition(ExpandableListView.getPackedPositionForGroup(section));
        }
    }

    // Gets called when scrolling the list manually
    @Override
    public int getSectionForPosition(int position) {
        return ExpandableListView.getPackedPositionGroup(mExpandableListView.getExpandableListPosition(position));
    }

    @Override
    public String[] getSections() {
    return groups;
    }

LogCat:
NullPointerException
FastScroller.getThumbPositionForListPosition(FastScroller.java:680)
FastScroller.onScroll(FastScroller.java:487)
at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:1337)

Can someone please guide me on this? I very appreciate for your time and help.

Thanks a lot

Community
  • 1
  • 1
Dante
  • 465
  • 2
  • 12

1 Answers1

0

You are overriding getSections to return your String array "groups", but the array does not appear to be initialized anywhere (unless it is done in code that you have not posted). mSections in FastScroller is initialized to the result of a getSections call, so you get a NullPointerException when FastScroller tries to access that field; the line giving you the NPE is

final int sectionCount = mSections.length;
EricaCooksey
  • 239
  • 1
  • 12
  • Thanks for the comment. I don't have groups array initialized anywhere in any other classes. I was just following the solution from the link in my question post. Obviously, something is missing, and I've tried to ask the person in that link a question, but no one has not yet replied. I don't quite understand how this workaround solution works.. Is there anyway can provide some example code? I don't have this code in my app: final int sectionCount = mSections.length; Thanks a lot – Dante Nov 19 '14 at 19:14
  • That code is from Android's FastScroller class -- that's where the exception is coming from. I'm not sure what "groups" should be initialized to based on your code, but try at least instantiating it in your constructor (i.e. `groups = new String[0];`) and it should at least eliminate that NPE. – EricaCooksey Nov 19 '14 at 20:39