android - 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' 上的 RecyclerView + ViewPager NullPointerException

标签 android nullpointerexception android-viewpager

我有一个扩展 AppCompatActivity 的 Activity ,其中包含一个包含 RecyclerView 的 fragment 。单击任何项​​目时,它将用另一个包含 ViewPager 的 fragment 替换此 fragment ,而 ViewPager 的 fragment 又是 RecyclerView。

RecyclerViewFragment
ViewPagerFragment

我在滚动页面时在 ViewPager fragment 上遇到以下错误(应用程序崩溃)。

E/InputEventReceiver: Exception dispatching input event. E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference E/MessageQueue-JNI: at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:2022) E/MessageQueue-JNI: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2059) E/MessageQueue-JNI: at android.view.ViewGroup.dispatchTransformedTouchEvent
.
.
.
E/AndroidRuntime: FATAL EXCEPTION: main E/AndroidRuntime: Process: me.twig.twigme, PID: 23451 E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference E/AndroidRuntime: at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:2022) E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2059) E/AndroidRuntime: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2432) E/AndroidRuntime: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)

下面是代码 RecyclerViewFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_cards, container, false);
}

@Override
public void onResume() {
    super.onResume();

    cardsRecyclerView = (RecyclerView) getActivity().findViewById(R.id.cards_recycler_view);
    final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    cardsRecyclerView.setLayoutManager(layoutManager);

    if(cardsRecyclerView.getAdapter() == null)
    {
        cardAdapter = new CardAdapter(getActivity().getApplicationContext(), getActivity(), new ArrayList<CardModel>());
        cardsRecyclerView.setAdapter(cardAdapter);
    }
    // List initialization code 
}

public static RecyclerViewFragment createInstance(String cardsJson) {

        RecyclerViewFragment cardsListFragment = new RecyclerViewFragment();
        Bundle args = new Bundle();
        args.putString("cardsJson", cardsJson);
        cardsListFragment.setArguments(args);
        return cardsListFragment;
}

ViewPagerFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_cards_pager, container, false);
}

@Override
public void onStart() {
    super.onStart();

    tabLayout = (TabLayout) getActivity().findViewById(R.id.tabLayout);
    tabLayout.removeAllTabs();

    viewPager = (ViewPager) getActivity().findViewById(R.id.viewPager);
    pagerAdapter = new PagerAdapter(getChildFragmentManager());

    for( i=0; i<3; i++)
    {
        pagerAdapter.addFragment(RecyclerViewFragment.createInstance(cardsJson), "Tab title");
    }

    viewPager.setAdapter(pagerAdapter);
    tabLayout.setupWithViewPager(viewPager);
}

class PagerAdapter extends FragmentStatePagerAdapter {

        private final List<Fragment> fragmentList = new ArrayList<>();
        private final List<String> fragmentTitleList = new ArrayList<>();

        public PagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
            fragmentList.clear();
            fragmentTitleList.clear();
        }

        public void addFragment(Fragment fragment, String title) {
            fragmentList.add(fragment);
            fragmentTitleList.add(title);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragmentTitleList.get(position);
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
}

fragment _卡片.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_frame_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/cards_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/ColorPrimaryLight"
        android:clipToPadding="false"
        xmlns:android="http://schemas.android.com/apk/res/android" />
</FrameLayout>

fragment_cards_pager.xml

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/coordinatorLayout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
    android:background="@color/ColorPrimaryLight">

    <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
        <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:tabTextColor="@android:color/white"
                app:tabSelectedTextColor="@android:color/white"
                app:tabIndicatorColor="@android:color/white"
                app:tabIndicatorHeight="6dp"/>
    </android.support.design.widget.AppBarLayout>

    <ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

最佳答案

来自 aga's answer

This issue usually occurs when no LayoutManager was provided for the RecyclerView. You can do it like so:

final LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);

此解决方案以编程方式完成,但这意味着您没有在 RecyclerView 中声明任何 LayoutManager。

关于android - 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' 上的 RecyclerView + ViewPager NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33109462/

相关文章:

android - 什么会导致 <p> 元素在 Android 浏览器中不跨越整个宽度

Android:如何使用 XML drawable 创建精确的 Material 设计阴影

java - 在 Google App Engine 上按最新创建的顺序接收实体

java - 跳过登录 Activity 时获取 NullPointerException

Java:程序启动时出现 ExceptionInInitializerError

android-fragments - 如何处理 FragmentPagerAdapter 重用 Fragments?

android - Flutter:在一组小部件中组合Draggable和GestureDetector

android - 尝试使用 Android Drawing 绘制图形时出现 NullPointerException

android - ViewPager 阻止加载下一个 View

Android viewpager weight - 全屏主题