title: How to open a specific fragment page in viewpager from another activity button click event
body: <p>Could any one let me know how to open a specific fragment present in a 
  viewpager from another activities button click event.</p>
  
  <p>I am using a alert dialog which shows two options
  to select respective screens in viewpager like below but I am getting null 
  pointer on clicking screentwo button which has code to open viewpagers second page.</p>
  
  <pre><code>public class MainActivity extends FragmentActivity {
      int pos;
      ViewPager pager;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);     
           pager = (ViewPager) findViewById(R.id.viewPager);
          pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
        Button   ds = (Button) findViewById(R.id.show_back);
       ds.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Intent goIntent = new Intent(MainActivity.this,SecondActivity.class);
              startActivity(goIntent);
  
          }
       });
      }
  
      private class MyPagerAdapter extends FragmentPagerAdapter {
  
          public MyPagerAdapter(FragmentManager fm) {
              super(fm);
          }
  
          @Override
          public Fragment getItem(int pos) {
              switch(pos) {
  
              case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
              case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
              default: return FirstFragment.newInstance("FirstFragment, Instance 1");
              }
          }
  
          @Override
          public int getCount() {
              return 2;
          }       
      }
  
  goesBack = (Button) findViewById(R.id.show_front);
  goesBack.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
          AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(SecondActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
          alertDialogBuilder.setMessage("Please choose your Favourite Screen");
          alertDialogBuilder.setNegativeButton("ScreenOne",new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
              Intent go = new Intent(SecondActivity.this,MainActivity.class);
              startActivity(go);
               }
          });
          alertDialogBuilder.setPositiveButton("ScreenTwo", new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface arg0, int arg1) {
                  // Create new fragment and transaction
                  SecondFragment newFragment = new SecondFragment();
                  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                  transaction.replace(R.id.container, newFragment, "NewFragment");
                  transaction.addToBackStack(null);
                  transaction.commit();
               }
          });
          AlertDialog alertDialog = alertDialogBuilder.create();
          alertDialog.show();         
      }
  });
  </code></pre>
  
  <p>Thanks &amp; Regards,</p>
  
  <p>Aditya.J</p>