title: Change page on button click ViewPager Android
body: <p><strong>UPDATES:</strong> <code>Logcat has been added to the question</code>
  I actually want to change view ( from one fragment to other. ) on <code>button Click</code> i have tried many things but I am unable to do it. I have also wrote a <code>CustomViewPager</code> to stop changing pages on swiping the screen. it is working fine.</p>
  
  <p>Here is my code:</p>
  
  <p><strong>MyFragment</strong></p>
  
  <pre><code> public CustomViewPager mViewPager;
   public Button next;
   public static int count=0;
  
   public static final MyFragment newInstance(String message,ArrayList&lt;String&gt; a)
  
   {
  
     MyFragment f = new MyFragment();
  
     Bundle bdl = new Bundle(1);
  
     bdl.putString(EXTRA_MESSAGE, message);
     bdl.putStringArrayList(EXTRA_ARRAY, a);
  
     f.setArguments(bdl);
  
     return f;
  
   }
  
  
  
   @Override
  
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
  
     Bundle savedInstanceState) {
  
     String message = getArguments().getString(EXTRA_MESSAGE);
     View v = inflater.inflate(R.layout.myfragment_layout, container, false);
  
     spinner = (Spinner)v.findViewById(R.id.spinoptions);
     answer = (EditText)v.findViewById(R.id.answer);
     next = (Button)v.findViewById(R.id.next);
  
     next.setOnClickListener(new OnClickListener() {
  
      @Override
      public void onClick(View v) {
          // TODO Auto-generated method stub
          mViewPager.setCurrentItem(1, true);//getItem(-1) for previous  
  
  // it is not working
  
      }
  });
  
     ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();
      list.add("list 1");
      list.add("list 2");
      list.add("list 3");
      Builtprofile.q.get(0).getOptions();
  
  
          answer.setVisibility(View.INVISIBLE);
          spinner.setVisibility(View.VISIBLE);
      ArrayAdapter&lt;String&gt; dataAdapter = new ArrayAdapter&lt;String&gt;(Builtprofile.context,
          android.R.layout.simple_spinner_item,getArguments().getStringArrayList(EXTRA_ARRAY));
      dataAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); 
         spinner.setAdapter(dataAdapter);
  
  
  
     TextView messageTextView = (TextView)v.findViewById(R.id.textView);
  
  
     messageTextView.setText(message);
  
  
  
     return v;
  
   }
   private int getItem() {
         return mViewPager.getCurrentItem();
  }
  </code></pre>
  
  <p><strong>Code for my PageAdapter</strong></p>
  
  <pre><code>class MyPageAdapter extends FragmentPagerAdapter {
  
        private List&lt;Fragment&gt; fragments;
  
  
        public MyPageAdapter(FragmentManager fm, List&lt;Fragment&gt; fragments) {
  
          super(fm);
  
          this.fragments = fragments;
  
        }
  
        @Override 
  
        public Fragment getItem(int position) {
  
          return this.fragments.get(position);
  
        }
  
  
        @Override
  
        public int getCount() {
  
          return this.fragments.size();
  
        }
  
      }
  </code></pre>
  
  <p><strong>Code for ActivityClass</strong></p>
  
  <pre><code>  public List&lt;Fragment&gt; getFragments(){
  
        List&lt;Fragment&gt; fList = new ArrayList&lt;Fragment&gt;();
  
       for(i=0;i&lt;q.size();i++)
       {
        fList.add(MyFragment.newInstance(q.get(i).getText(),q.get(i).getOptions()));
  
       }
  
  
        return fList;
  
      }
  
    public void fragmentopener()
    {
        List&lt;Fragment&gt; fragments = getFragments();
  
          pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
  
          ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
  
          pager.setAdapter(pageAdapter);
    }
  </code></pre>
  
  <p><strong>Code for myCustomViewPager</strong></p>
  
  <pre><code>public class CustomViewPager extends ViewPager {
  
      private boolean isPagingEnabled;
  
  
      public CustomViewPager(Context context, AttributeSet attrs) {
          super(context, attrs);
          this.isPagingEnabled = false;
      }
  
      @Override
      public boolean onTouchEvent(MotionEvent event) {
          if (this.isPagingEnabled) {
              return super.onTouchEvent(event);
          }
  
          return false;
      }
  
      @Override
      public boolean onInterceptTouchEvent(MotionEvent event) {
          if (this.isPagingEnabled) {
              return super.onInterceptTouchEvent(event);
          }
  
          return false;
      }
  
      public void setPagingEnabled(boolean b) {
          this.isPagingEnabled = b;
      }
  }
  </code></pre>
  
  <p><strong>LogCat:</strong></p>
  
  <pre><code>04-07 15:55:53.254: E/AndroidRuntime(17379): FATAL EXCEPTION: main
  04-07 15:55:53.254: E/AndroidRuntime(17379): java.lang.NullPointerException
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at adapters.MyFragment$1.onClick(MyFragment.java:71)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.view.View.performClick(View.java:3574)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.view.View$PerformClick.run(View.java:14293)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.os.Handler.handleCallback(Handler.java:605)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.os.Handler.dispatchMessage(Handler.java:92)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.os.Looper.loop(Looper.java:137)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at android.app.ActivityThread.main(ActivityThread.java:4448)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at java.lang.reflect.Method.invokeNative(Native Method)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at java.lang.reflect.Method.invoke(Method.java:511)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
  04-07 15:55:53.254: E/AndroidRuntime(17379):    at dalvik.system.NativeStart.main(Native Method)
  </code></pre>