title: View Pager addOnPageChangeListener doesn't work with Fragment Pager Adapter
body: <p>I'm having trouble implementing "addOnPageChangeListener" for my viewPager. I'm essentially implementing <a href="https://developer.android.com/training/animation/screen-slide" rel="nofollow noreferrer">THIS</a>; its a slider view with fragments, I'm doing the same with <strong>child fragments</strong>. </p>
  
  <p>When I add the viewListener to my ViewPager, my app crashes. It works fine without the addOnPageListener. I want to do this so I can put some title animations for my actionbar and maybe some background effects.</p>
  
  <p>This is what it looks like:
  <a href="https://i.stack.imgur.com/XsEyR.jpg" rel="nofollow noreferrer">View Pager App Screenshot</a>.</p>
  
  <p>Any help or suggestions would be appreciated, thanks!</p>
  
  <p>LogCat:
  <a href="https://pastebin.com/xqsBQ46h" rel="nofollow noreferrer">https://pastebin.com/xqsBQ46h</a></p>
  
  <p>JavaCode:</p>
  
  <pre><code>package com.example.varungovind.tm_alpha000;
  
  import android.os.Bundle;
  ...
  
  public class HomeFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener{
  
      private DrawerLayout mDrawer;
      private ViewPager mViewPager;
      private SamplePagerAdapter mPagerAdapter;
      private Toolbar mToolBar;
  
      @Nullable
      @Override
      public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_home, container, false);
  
          //sets the system bar transparent
          Objects.requireNonNull(getActivity()).getWindow().getDecorView().setSystemUiVisibility(
                  View.SYSTEM_UI_FLAG_VISIBLE);
  
  
  
          //ToolBar and Navigation Drawer
          mToolBar = view.findViewById(R.id.fragment_toolbar);
          ((AppCompatActivity)getActivity()).setSupportActionBar(mToolBar);
          mDrawer = view.findViewById(R.id.nav_drawer_layout);
          NavigationView navigationView = view.findViewById(R.id.nav_view);
          navigationView.setNavigationItemSelectedListener(this);
          ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(getActivity(), mDrawer, mToolBar, R.string.nav_drawer_open, R.string.nav_drawer_close);
          mDrawer.addDrawerListener(toggle);
          toggle.syncState();
  
          //Setting toolbar title
  
  
          ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("");
          final Animation fade_in = AnimationUtils.loadAnimation(getContext(), R.anim.fade_in);
          final Animation fade_out = AnimationUtils.loadAnimation(getContext(), R.anim.fade_out);
          final TextView toolbar_title = view.findViewById(R.id.toolbar_title);
  
          //This line of code causes the error
          mViewPager.addOnPageChangeListener(viewListener);
  
          //SliderViewPager
          mViewPager = view.findViewById(R.id.fragment_home_view_pager);
          mPagerAdapter = new SamplePagerAdapter(getChildFragmentManager());
          mViewPager.setAdapter(mPagerAdapter);
          mViewPager.setPageTransformer(false, new ZoomOutPageTransformer());
  
          return view;
      }
  
      ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
          @Override
          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
  
          @Override
          public void onPageSelected(int position) {}
  
          @Override
          public void onPageScrollStateChanged(int state) {}
      };
  
      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
          switch (menuItem.getItemId()) {
              case R.id.nav_about:
                  break;
              case R.id.nav_donate:
                  break;
              case R.id.nav_done:
                  mViewPager.setCurrentItem(1, true);
                  break;
              case R.id.nav_upcoming:
                  mViewPager.setCurrentItem(0, true);
                  break;
              case R.id.nav_feedback:
                  break;
              case R.id.nav_drawer_new_list:
                  break;
              case R.id.nav_settings:
                  break;
          }
          mDrawer.closeDrawer(GravityCompat.START);
  
          return true;
      }
  
      private class SamplePagerAdapter extends FragmentPagerAdapter {
          public SamplePagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public CFragment getItem(int position) {
              /** Show a Fragment based on the position of the current screen */
              if (position == 0) {
                  return new Upcoming_ChildFragment();
              } else
                  return new Done_ChildFragment();
          }
  
          @Override
          public int getCount() {
              // Show 2 total pages.
              return 2;
          }
  
      }
  }
  </code></pre>