我有三个标签,所有在一个tablayout
,在Tab1,有一个RecycleView
.
今天我想向它添加SwipeRefreshLayout:
在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; }
不幸的是,当我从tab1滚动到tab2,然后回到tab1时,活动崩溃:
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)
但显然我已经设置了LayoutManger!似乎交错的网格布局已经消失了......
更多Weired是,当我只是单击tab1,tab2时,Activity不会崩溃,但是当我从tab2滚动到tab1时,它崩溃了
发生了什么?谢谢!
加上整个错误日志:
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)
小智.. 7
将您的recyclerView
作业移至onViewCreated()
而不是onCreateView()
.问题是当你TabFragment
在viewPager滚动上分离并重新附加时.
将您的recyclerView
作业移至onViewCreated()
而不是onCreateView()
.问题是当你TabFragment
在viewPager滚动上分离并重新附加时.