title: The viewpager in a fragment
body: <p>I want do some slides in my app, in the activity, the following code works fine, but in the fragment, the app crash and say something like "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference", how can I fix this? </p>
  
  <p>The code in an activity:</p>
  
  <pre><code>private ViewPager frViewPager;
  private int[] frImgIds = new int[]{R.drawable.pic_1,R.drawable.pic_2,R.drawable.pic_3};
  private List&lt;ImageView&gt; mImages = new ArrayList&lt;ImageView&gt;();
  
  frViewPager = (ViewPager) findViewById(R.id.landing_page);
  frViewPager.setAdapter(new PagerAdapter() {
      public Object instantiateItem(ViewGroup container, int position) {
          ImageView imageView = new ImageView(LandingPageActivity.this);
          imageView.setImageResource(frImgIds[position]);
          imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          container.addView(imageView);
          mImages.add(imageView);
          return imageView;
      }
      /* Other override code……*/
  })
  </code></pre>
  
  <p>The code in a fragment:</p>
  
  <pre><code>private ViewPager frViewPager;
  private int[] frImgIds = new int[]{R.drawable.pic_1,R.drawable.pic_2,R.drawable.pic_3};
  private List&lt;ImageView&gt; mImages = new ArrayList&lt;ImageView&gt;();
  
  frViewPager = (ViewPager) this.getActivity().findViewById(R.id.recommend);
  frViewPager.setAdapter(new PagerAdapter() {
      public Object instantiateItem(ViewGroup container, int position) {
          ImageView imageView = new ImageView(getActivity());
          imageView.setImageResource(frImgIds[position]);
          imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
          container.addView(imageView);
          mImages.add(imageView);
          return imageView;
      }
      /* Other override code……*/
  })
  </code></pre>
  
  <p>Update:</p>
  
  <pre><code>04-16 02:04:33.951    1996-1996/com.demo.example E/AndroidRuntime﹕ FATAL EXCEPTION: main
  Process: com.demo.example, PID: 1996
  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.demo.example/com.demo.android.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
          at android.app.ActivityThread.access$800(ActivityThread.java:144)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5221)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.ViewPager.setAdapter(android.support.v4.view.PagerAdapter)' on a null object reference
          at com.demo.android.fragment.IndexFragment.onCreateView(IndexFragment.java:32)
          at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
          at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
          at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
          at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
          at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
          at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1220)
          at android.app.Activity.performStart(Activity.java:5953)
          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
          at android.app.ActivityThread.access$800(ActivityThread.java:144)
          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:135)
          at android.app.ActivityThread.main(ActivityThread.java:5221)
          at java.lang.reflect.Method.invoke(Native Method)
          at java.lang.reflect.Method.invoke(Method.java:372)
          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
  </code></pre>