当加载更多的android快速滚动时,RecyclerView崩溃

时间:2016-04-26 11:31:23

标签: android android-recyclerview

我在RecyclerView上实施了更多的加载。正常滚动时它工作正常。但是当你快速滚动时(如超快),它会崩溃并提供以下日志

java.lang.NullPointerException: Attempt to read from field 'int android.support.v7.widget.RecyclerView$ItemAnimator$ItemHolderInfo.left' on a null object reference
at android.support.v7.widget.SimpleItemAnimator.animateDisappearance(SimpleItemAnimator.java:78)
at android.support.v7.widget.RecyclerView.animateDisappearance(RecyclerView.java:3246)
at android.support.v7.widget.RecyclerView.access$700(RecyclerView.java:147)
at android.support.v7.widget.RecyclerView$4.processDisappeared(RecyclerView.java:422)
at android.support.v7.widget.ViewInfoStore.process(ViewInfoStore.java:231)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3086)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2914)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3277)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1087)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:797)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:811)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16636)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)

在android.view.Choreographer.doFrame(Choreographer.java:606)

以下是我设置RecyclerView

的代码
NetworkManager.getInstance(this).post(Constants.urlBuilderForListing("get_home_videos", "0"), new RequestResponseListener<JSONObject>() {
        @Override
        public void onResult(Boolean isSuccess, JSONObject Object) {
            loader.setVisibility(View.GONE);
            try {
                if (isSuccess != null) {
                    if (isSuccess) {
                        list.setVisibility(View.VISIBLE);
                        if (Object.getInt("success") > 0) {
                            videos = new ArrayList<>();
                            JSONArray jarray = Object.getJSONArray("msg");
                            for (int i = 0; i < jarray.length(); i++) {
                                JSONObject jObj = jarray.getJSONObject(i);
                                Video video = new Video(jObj.getString("id"),
                                        jObj.getString("cat_id"),
                                        jObj.getString("cat_name"),
                                        jObj.getString("name")
                                       );
                                videos.add(video);
                            }
                            if (videos.size() > 0) {
                                adapter = new ListAdapter(videos, list, true, video);
                                adapter.setOnLoadMoreListener(PlayerActivity.this);
                                list.setAdapter(adapter);
                            }
                        } else {
                            Utils.showSnackBar(view, "No Videos to show right now.");
                        }
                    }
                } else {
                    Utils.showSnackBar(view, "No Internet.");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

这是在调用OnLoadMore的时候

offset += 10;
    try {
        videos.add(null);
        adapter.notifyItemInserted(videos.size() - 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    NetworkManager.getInstance(this).post(Constants.urlBuilderForListing("get_home_videos", String.valueOf(offset)), new RequestResponseListener<JSONObject>() {
        @Override
        public void onResult(Boolean isSuccess, JSONObject Object) {

            videos.remove(videos.size() - 1);
            adapter.notifyItemRemoved(videos.size());
            adapter.setLoaded();

            try {
                if (isSuccess != null) {
                    if (isSuccess) {
                        list.setVisibility(View.VISIBLE);
                        if (Object.getInt("success") > 0) {

                            JSONArray jarray = Object.getJSONArray("msg");
                            for (int i = 0; i < jarray.length(); i++) {
                                JSONObject jObj = jarray.getJSONObject(i);
                                Video video = new Video(jObj.getString("id"),
                                        jObj.getString("cat_id"),
                                        jObj.getString("cat_name"),
                                        jObj.getString("name"));
                                videos.add(video);
                                adapter.notifyDataSetChanged();
                            }

                        } else {
                            Utils.showSnackBar(view, "End of list.");
                        }
                    }
                } else {
                    Utils.showSnackBar(view, "No Internet.");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

这里是在xml中实现recyclerview

 <android.support.v7.widget.RecyclerView
                android:id="@+id/list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbars="vertical"
                android:visibility="gone"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

适配器与每个适配器相同。

所以请有人告诉我为什么我会收到这个错误,以及解决这个问题的可行方法是什么。

感谢。

0 个答案:

没有答案