title: How to go other Tabs by clicking on a Button from the current Tab in Android?
body: <p>I trying to write a code in Android, to switch from one tab to another tab by click on a button.</p>
  
  <p>I know to by clicking on tab we can switch from one tab to another but can it be possible to switch from one tab to another tab by clicking on one button.</p>
  
  <p>I tried the following tutorial.</p>
  
  <p><a href="http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/" rel="noreferrer">http://www.androidhive.info/2013/10/android-tab-layout-with-swipeable-views-1/</a></p>
  
  <p>In this tutorial i have edited the MovieFragment.java file in following manner.</p>
  
  <p><strong>MovieFragment.java</strong></p>
  
  <pre><code>package info.androidhive.tabsswipe;
  
  import info.androidhive.tabsswipe.R;
  import android.annotation.SuppressLint;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager; 
  import android.support.v4.view.ViewPager; 
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Button;
  
  public class MoviesFragment extends Fragment {
  
  ViewPager viewPager;
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle    savedInstanceState) 
  {
  
     View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
  
     Button btn = (Button) rootView.findViewById(R.id.btn);
  
     btn.setOnClickListener(new View.OnClickListener() 
     {
  
         @Override
        public void onClick(View v) 
        {
  
            viewPager.setCurrentItem(0);
  
        }
    }); 
  
  
   return rootView;
  }}
  </code></pre>
  
  <p><strong>XML Code :</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
    &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:orientation="vertical" 
       android:background="#17df0d"&gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/myfivepanelpager"
      android:layout_width="match_parent"
      android:layout_height="match_parent" /&gt;
  
  
  &lt;TextView android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="Design Movies Screen"
      android:textSize="20dp"
      android:layout_centerInParent="true"/&gt;
  
  &lt;Button 
      android:id="@+id/btn"
      android:layout_width="100dp"
      android:layout_height="80dp"
      android:text="Click"
      /&gt;
  
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>I have created a button in the xml layout and trying switch to tab -1 but i am getting Null Pointer Exception.</p>
  
  <p><strong>Error Code:</strong></p>
  
  <pre><code>05-20 09:07:10.995: E/AndroidRuntime(1117): FATAL EXCEPTION: main
  05-20 09:07:10.995: E/AndroidRuntime(1117): java.lang.NullPointerException
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at info.androidhive.tabsswipe.MoviesFragment$1.onClick(MoviesFragment.java:33)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.view.View.performClick(View.java:4240)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at  android.view.View$PerformClick.run(View.java:17721)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Handler.handleCallback(Handler.java:730)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Handler.dispatchMessage(Handler.java:92)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.os.Looper.loop(Looper.java:137)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at android.app.ActivityThread.main(ActivityThread.java:5103) 
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invokeNative(Native Method)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at java.lang.reflect.Method.invoke(Method.java:525) 
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  05-20 09:07:10.995: E/AndroidRuntime(1117):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Please suggest me some solution or some good technique to do it.</p>