title: Access to view into Fragment
body: <p>I'm trying to start an animation when the fragment is showed on screen(I'm working with a ViewPager).</p>
  
  <p>This a part of my code</p>
  
  <pre><code>public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
  
          private Map&lt;Integer, String&gt; mFragmentTags;
          private FragmentManager mFragmentManager;
  
          public ScreenSlidePagerAdapter(FragmentManager fm) {
              super(fm);
              mFragmentManager = fm;
              mFragmentTags = new HashMap&lt;Integer, String&gt;();
          }
  
          public ScreenSlidePageFragment getFragment(int position) {
              String tag = mFragmentTags.get(position);
              if (tag == null)
                  return null;
              return (ScreenSlidePageFragment) mFragmentManager.findFragmentByTag(tag);
          }
  
          @Override
          public Fragment getItem(int position) {
              return ScreenSlidePageFragment.create(position);
          }
  
          @Override
          public int getCount() {
              return NUM_PAGES;
          }
  
  
  
          @Override
          public Object instantiateItem(ViewGroup container, int position) {
              Object obj = super.instantiateItem(container, position);
              if (obj instanceof Fragment) {
                  // record the fragment tag here.
                  Fragment f = (Fragment) obj;
                  String tag = f.getTag();
                  mFragmentTags.put(position, tag);
                  System.out.println("holi");
              }
              return obj;
          }
  
      }
  </code></pre>
  
  <hr>
  
  <pre><code>    public class ScreenSlideActivity extends FragmentActivity {
      (...)
      @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_screen_slide);
  
              // Instantiate a ViewPager and a PagerAdapter.
              mPager = (ViewPager) findViewById(R.id.pager);
              mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
  
      (...)
  
      SimpleOnPageChangeListener mPageChangelistener = new ViewPager.SimpleOnPageChangeListener() {
                  @Override
                  public void onPageSelected(int position) {
                      // When changing pages, reset the action bar actions since they are dependent
                      // on which page is currently active. An alternative approach is to have each
                      // fragment expose actions itself (rather than the activity exposing actions),
                      // but for simplicity, the activity provides the actions in this sample.  
                      ScreenSlidePageFragment currentFgm = mPagerAdapter.getFragment(position);
                      Animation animacion = AnimationUtils.loadAnimation(ScreenSlideActivity.this, R.anim.glasses);
                      currentFgm.getView().findViewById(R.id.image).startAnimation(animacion);
                      invalidateOptionsMenu();
                  }
  (...)
  </code></pre>
  
  <p>The problem is when <code>onPageSelected</code> is called returns a NullPointer Exception in</p>
  
  <pre><code>currentFgm.getView().findViewById(R.id.image).startAnimation(animacion);
  </code></pre>
  
  <p>How could I start the animation when the fragment is showed on screen?</p>