0
votes

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

1
which SDK level are you using? - EricaCooksey
I am using the latest Eclipse/ADT since September 2014. Target Android 4.3 - Dante

1 Answers

0
votes

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;