我有三个标签,全部位于tablayout
,在Tab1中,有一个RecycleView
。
今天我想向它添加SwipeRefreshLayout:
<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>
在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!看来,staggeredgridlayout已经消失......
还有更多已经,当我点击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)
答案 0 :(得分:7)
将recyclerView
作业移至onViewCreated()
而不是onCreateView()
。问题是当TabFragment
分离并重新附加到viewPager滚动时。