cymcsg / UltimateRecyclerView

A RecyclerView(advanced and flexible version of ListView in Android) with refreshing,loading more,animation and many other features.
Apache License 2.0
7.22k stars 1.43k forks source link

NullPointerException #418

Open zeshaoaaa opened 8 years ago

zeshaoaaa commented 8 years ago

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.widget.RecyclerView$LayoutManager.getChildCount()' on a null object reference at com.marshalchen.ultimaterecyclerview.uiUtils.RecyclerViewPositionHelper.findFirstVisibleItemPosition(RecyclerViewPositionHelper.java:50) at com.marshalchen.ultimaterecyclerview.UltimateRecyclerView.scroll_load_more_detection(UltimateRecyclerView.java:436) at com.marshalchen.ultimaterecyclerview.UltimateRecyclerView.access$300(UltimateRecyclerView.java:61) at com.marshalchen.ultimaterecyclerview.UltimateRecyclerView$2.onScrolled(UltimateRecyclerView.java:498) at android.support.v7.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:4250) at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3348) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3080) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:598) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1195) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639) at android.widget.FrameLayout.onLayout(FrameLayout.java:574) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813) at android.widget.LinearLayout.onLayout(LinearLayout.java:1722) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813) at android.widget.LinearLayout.onLayout(LinearLayout.java:1722) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1195) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639) at android.widget.FrameLayout.onLayout(FrameLayout.java:574) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813) at android.widget.LinearLayout.onLayout(LinearLayout.java:1722) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639) at android.widget.FrameLayout.onLayout(FrameLayout.java:574) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1959) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1813) at android.widget.LinearLayout.onLayout(LinearLayout.java:1722) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:639) at android.widget.FrameLayout.onLayout(FrameLayout.java:574) at android.view.View.layout(View.java:16001) at android.view.ViewGroup.layout(ViewGroup.java:5181) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2459) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2156) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1274)


How to fix it ?

sprhawk commented 7 years ago
    mUltimateRecyclerView = (UltimateRecyclerView)rootview.findViewById(R.id.business_list_view);
    // Due to some problem, It needs to set LayoutManger first,
    // or else in setHasFixedSize it will crash for underlying
    // calls getLayoutManger will get null
    mLinearLayout = new LinearLayoutManager(this.getActivity());
    mUltimateRecyclerView.setLayoutManager(mLinearLayout);

    mUltimateRecyclerView.setHasFixedSize(true);

you need to put the setLayoutManager before (at least) setHasFixedSize,

Calion54 commented 7 years ago

I got the same problem. When I set a GridLayoutManager that work but with LinearLayoutManager this error appears. Has anyone fix it?

Calion54 commented 7 years ago

I have found the solution : You need assign directly your LayoutManager (before the adapter...)

example :

rv = (UltimateRecyclerView) rootView.findViewById(R.id.rv_recycler_view);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
rv.setLayoutManager(linearLayoutManager);

setInstructionAdapter = new SetInstructionAdapter(context, setInstructionList);
rv.setAdapter(setInstructionAdapter);

rv.setHasFixedSize(true);
gindoc commented 7 years ago

@Calion54 You are right.I have the same problem, but if I set the LayoutManager before setAdapter, it works.