I have three tabs who all in a tablayout
, in Tab1, there is a RecycleView
.
Today I want to add SwipeRefreshLayout to it:
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swiperefreshlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>
In Tab1Fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main_list, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
recyclerView.setLayoutManager(staggeredGridLayoutManager);
swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swiperefreshlayout);
swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
android.R.color.holo_orange_light, android.R.color.holo_red_light);
return view;
}
Unfortunately, When I scroll from tab1 to tab2, and back to tab1, the activity crashes:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean
android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1613)
at android.view.View.canScrollVertically(View.java:12880)
But obviously I had set LayoutManger! It appeared that the staggeredgridlayout is gone...
What more Weired is that, when I just click tab1, tab2, Activity does not crash, But when I scroll from tab2 to tab1, it crashed
What happened? Thank you!
PLUS the WHOLE error log:
12-02 16:57:59.857 28189-28189/xyz.doutu.doutu E/AndroidRuntime: Process: xyz.doutu.doutu, PID: 28189
ava.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference
at android.support.v7.widget.RecyclerView.computeVerticalScrollOffset(RecyclerView.java:1613)
at android.view.View.canScrollVertically(View.java:12880)
at android.support.v4.view.ViewCompatICS.canScrollVertically(ViewCompatICS.java:35)
at android.support.v4.view.ViewCompat$ICSViewCompatImpl.canScrollVertically(ViewCompat.java:1253)
at android.support.v4.view.ViewCompat.canScrollVertically(ViewCompat.java:1695)
at android.support.v4.widget.SwipeRefreshLayout.canChildScrollUp(SwipeRefreshLayout.java:646)
at android.support.v4.widget.SwipeRefreshLayout.onInterceptTouchEvent(SwipeRefreshLayout.java:660)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1961)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2408)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2050)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2390)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1734)
at android.app.Activity.dispatchTouchEvent(Activity.java:2833)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2351)
at android.view.View.dispatchPointerEvent(View.java:8700)
Move your recyclerView
assignment to onViewCreated()
instead of onCreateView()
. The problem is when your TabFragment
gets detached and re-attached on viewPager scroll.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With