0

我正在尝试在 ExpandableListView 中启用快速滚动,并且我已经尝试了此链接中的解决方法:android fastScroll 仅涵盖列表的一部分

问题是,每当我展开足够多的组以触发快速滚动条时,它都会给我 NullPointerException 错误。我试过以下代码:

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)

有人可以指导我吗?我非常感谢您的时间和帮助。

非常感谢

4

1 回答 1

0

您正在重写getSections以返回您的字符串数组“组”,但该数组似乎没有在任何地方初始化(除非它是在您未发布的代码中完成的)。mSections在 FastScroller 中被初始化为 getSections 调用的结果,因此当 FastScroller 尝试访问该字段时,您会收到 NullPointerException;给你 NPE 的行是

final int sectionCount = mSections.length;
于 2014-11-18T22:09:49.147 回答