0

我試圖啓用ExpandableListView快速滾動,我已經試過此鏈接中的解決方法解決:android fastScroll only covers part of the list如何讓FastScroll使用ExpandableListView?

的問題是,它給了我的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) 

有人可以請指導我嗎? 我非常感謝您的時間和幫助。

非常感謝

+0

您使用哪個SDK級別? – EricaCooksey 2014-11-18 19:47:57

+0

自2014年9月起,我使用最新的Eclipse/ADT。目標Android 4.3 – Dante 2014-11-18 20:19:17

回答

0

要覆蓋getSections來回報您的字符串數組「團體」,但數組不會出現在任何地方進行初始化(除非它是在你沒有張貼代碼完成)。 FastScroller中的mSections被初始化爲getSections調用的結果,所以當FastScroller嘗試訪問該字段時會得到NullPointerException;給你NPE的線路是

final int sectionCount = mSections.length; 
+0

感謝您的評論。我沒有在任何其他類中的任何位置初始化組數組。我只是在我的問題帖子中關注鏈接中的解決方案。很顯然,有些東西缺失了,我試圖向這個環節中的人提出一個問題,但沒有人還沒有回答。我不太明白這個解決方案的解決方案是如何工作的。有無論如何可以提供一些示例代碼?我沒有在我的應用程序代碼:final int sectionCount = mSections.length;非常感謝 – Dante 2014-11-19 19:14:00

+0

該代碼來自Android的FastScroller類 - 這是異常來自的地方。我不確定應該根據你的代碼初始化什麼「組」,但至少應該在你的構造函數中實例化它(即'groups = new String [0];'),它至少應該消除NPE。 – EricaCooksey 2014-11-19 20:39:54