gpt4 book ai didi

android - RecycleView 在 Fragment 中崩溃,因为 LayoutManager 为 NULL

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:27:36 27 4
gpt4 key购买 nike

我有三个标签,它们都在一个 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 时, Activity 崩溃了:

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!看起来交错的网格布局不见了......

奇怪的是,当我只点击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)

最佳答案

将您的 recyclerView 分配移动到 onViewCreated() 而不是 onCreateView()。问题是当您的 TabFragment 分离并重新附加到 viewPager 滚动条上时。

关于android - RecycleView 在 Fragment 中崩溃,因为 LayoutManager 为 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34037101/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com