title: RecyclerView + ViewPager NullPointerException on 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()'
body: <p>I have an activity extending AppCompatActivity with a single fragment which contains RecyclerView. On any item click, it will replace this fragment with another fragment which contains ViewPager and fragments of ViewPager are again RecyclerView.</p>
  
  <p><a href="http://i.stack.imgur.com/n99ij.png" rel="nofollow">RecyclerViewFragment</a><br>
  <a href="http://i.stack.imgur.com/0zhG7.png" rel="nofollow">ViewPagerFragment</a></p>
  
  <p>I am getting following error(app crashes) on ViewPager fragment while scrolling the page.</p>
  
  <blockquote>
    <p>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<br>
    .<br>
    .<br>
    .<br>
    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)</p>
  </blockquote>
  
  <p>Below is the code
  RecyclerViewFragment</p>
  
  <pre><code>@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&lt;CardModel&gt;());
          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;
  }
  </code></pre>
  
  <p>ViewPagerFragment</p>
  
  <pre><code>@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&lt;3; i++)
      {
          pagerAdapter.addFragment(RecyclerViewFragment.createInstance(cardsJson), "Tab title");
      }
  
      viewPager.setAdapter(pagerAdapter);
      tabLayout.setupWithViewPager(viewPager);
  }
  
  class PagerAdapter extends FragmentStatePagerAdapter {
  
          private final List&lt;Fragment&gt; fragmentList = new ArrayList&lt;&gt;();
          private final List&lt;String&gt; fragmentTitleList = new ArrayList&lt;&gt;();
  
          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;
          }
  }
  </code></pre>
  
  <p>fragment_cards.xml</p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;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" /&gt;
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p>fragment_cards_pager.xml</p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;android.support.design.widget.AppBarLayout
              android:id="@+id/appBarLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"&gt;
          &lt;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"/&gt;
      &lt;/android.support.design.widget.AppBarLayout&gt;
  
      &lt;ViewPager
              android:id="@+id/viewPager"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:layout_behavior="@string/appbar_scrolling_view_behavior"/&gt;
  &lt;/android.support.design.widget.CoordinatorLayout&gt;
  </code></pre>