java.lang.NullPointerException\nandroid.support.v7.widget.StaggeredGridLayoutManager$Span.access$300

写瀑布流的时候,遇到了如下crash:

Attempt to read from field 'java.util.ArrayList android.support.v7.widget.StaggeredGridLayoutManager$Span.mViews' on a null object reference
10-20 21:04:17.826 5662-5662/com.jingdong.app.mall W/System.err:     at android.support.v7.widget.StaggeredGridLayoutManager$Span.access$300(StaggeredGridLayoutManager.java:2094)
10-20 21:04:17.826 5662-5662/com.jingdong.app.mall W/System.err:     at android.support.v7.widget.StaggeredGridLayoutManager.recycleFromEnd(StaggeredGridLayoutManager.java:1761)
10-20 21:04:17.826 5662-5662/com.jingdong.app.mall W/System.err:     at android.support.v7.widget.StaggeredGridLayoutManager.recycle(StaggeredGridLayoutManager.java:1585)
10-20 21:04:17.826 5662-5662/com.jingdong.app.mall W/System.err:     at android.support.v7.widget.StaggeredGridLayoutManager.fill(StaggeredGridLayoutManager.java:1514)
10-20 21:04:17.826 5662-5662/com.jingdong.app.mall W/System.err:     at android.support.v7.widget.StaggeredGridLayoutManager.scrollBy(StaggeredGridLayoutManager.java:1926)
10-20 21:04:17.826 5662-5662/com.jingdong.app.mall W/System.err:     at android.support.v7.widget.StaggeredGridLayoutManager.scrollVerticallyBy(StaggeredGridLayoutManager.java:1846)

折腾了好久才弄好,现记录一下:

当ViewPager、RecyclerView和StaggeredGridLayoutManager配合用的时候,左右滑动,会出现这个crash

原因是当来回切换的时候,假设切换页面顺序是0->1->2->3->4->5->4->3->2->1->0,当重新切换到0的时候,viewpager可能已经把页面0的view给回收了,而recyclerview和StaggeredGridLayoutManager认为页面0的view还在,双重回收机制造成了上述空指针错误。

解决办法就是在onCreateView里判断rootView如果已存在,就重新new一个StaggeredGridLayoutManager

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (rootView == null) {
        rootView = ImageUtil.inflate(getContext(), R.layout.main__fragment, null);
        initView(rootView);
    }else{
        staggeredGridLayoutManager = new JShowStaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        pullToRefreshRecyclerView.getRefreshableView().setLayoutManager(staggeredGridLayoutManager);
    }
    return rootView;
}

这样,来回切换时不会崩溃,但是不能保存上次的滑动状态

解决办法:

重写recyclerview,在离开屏幕时保存一下状态,并加一个恢复状态的方法供fragment调用

@Override
protected void onDetachedFromWindow() {
    LayoutManager layoutManager = getLayoutManager();
    if(StaggeredGridLayoutManager.class.isInstance(layoutManager)){
        saveStates = ((StaggeredGridLayoutManager)layoutManager).onSaveInstanceState();
    }
    super.onDetachedFromWindow();
}
public void restoreSaveStateIfPossible(){
    if(saveStates != null){
        LayoutManager layoutManager = getLayoutManager();
        if(StaggeredGridLayoutManager.class.isInstance(layoutManager)){
            if(saveStates != null) {
                layoutManager.onRestoreInstanceState(saveStates);
            }
        }
    }
}
在fragment里:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d(TAG,"onCreateView"+",cateId:"+cateId);
    if (rootView == null) {
        rootView = ImageUtil.inflate(...);
        //初始化view
    }else{
        staggeredGridLayoutManager = new JShowStaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
        pullToRefreshRecyclerView.getRefreshableView().setLayoutManager(staggeredGridLayoutManager);
        recyclerview.restoreSaveStateIfPossible();
    }
    return rootView;
}
好,问题解决!撒花~


你可能感兴趣的:(Android)