Change page on button click ViewPager Android

you get NPE on line 71 @ MyFragment, as you said in comment this line is:

 mViewPager.setCurrentItem(1, true);

so mViewPager is null, you need initialize that before using

use following code instead your code:

mViewPager.setCurrentItem(getItem(+1), true);

and getItem() is:

private int getItem(int i) {
       return mViewPager.getCurrentItem() + i;
}

Not an Answer though, but read on

I might be late here, I found a very easy solution from an online resource:

In the fragment xml, where you are implementing the next / Previous button add this line to the button xml:

android:onClick="jumpToPage"

Place the cursor inbetween "jumpToPage" - press Alt + Enter:

enter image description here

Add the related function in your PagerActivity -- Not in the fragment

Now modify the function to this:

public void jumpToPage(View view) {

        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
    }

Now buy me a nunchai :)


I hope this will help you:

Just put this code on click of button:

pager.setCurrentItem(1, true);

NOTE: in the place of 1 you can pass your number