I am trying to get my activity to make a call to my fragment when my viewpager detects any swiping.
Here is my activity code.
public interface SwipeListener { void swipe(); } private SwipeListener mSwipeListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_catalog); ButterKnife.bind(this); setSupportActionBar(mToolbar); int startingFragmentPosition = 0; mSwipeListener = (SwipeListener) this; ClothingSectionsPagerAdapter sectionsPagerAdapter = new ClothingSectionsPagerAdapter(getSupportFragmentManager()); sectionsPagerAdapter.setContext(this); mViewPager.setAdapter(sectionsPagerAdapter); mTabLayout.setupWithViewPager(mViewPager); mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout)); mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager)); mViewPager.setCurrentItem(startingFragmentPosition); mViewPager.setOnDragListener(new View.OnDragListener() { @Override public boolean onDrag(View view, DragEvent dragEvent) { mSwipeListener.swipe(); return false; } }); } In my fragment I implement this interface and override the method. However when I run the app. It crashes, saying that it cannot cast this to SwipeListener in the onCreate method. How do I fix this?