title: NullPointerException on setMenuVisibility
body: <p>I'm following the next tutorial to insert a tab <a href="http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/" rel="nofollow">http://www.truiton.com/2015/06/android-tabs-example-fragments-viewpager/</a>. My problem is that I'm trying to insert it in a fragment not in an activity, but I'm getting the next error.</p>
  
  <pre><code>java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.Fragment.setMenuVisibility(boolean)' on a null object reference
     at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:116)
     at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:943)
     at android.support.v4.view.ViewPager.populate(ViewPager.java:1157)
     at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
     at android.view.View.measure(View.java:18797)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
     at android.view.View.measure(View.java:18797)
     at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1283)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
     at android.widget.ScrollView.onMeasure(ScrollView.java:340)
     at android.view.View.measure(View.java:18797)
     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
     at android.view.View.measure(View.java:18797)
     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
     at android.view.View.measure(View.java:18797)
     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:715)
     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
     at android.view.View.measure(View.java:18797)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
     at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
     at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:735)
     at android.view.View.measure(View.java:18797)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
     at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
     at android.view.View.measure(View.java:18797)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
     at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:391)
     at android.view.View.measure(View.java:18797)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
     at android.view.View.measure(View.java:18797)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
     at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
     at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
     at android.view.View.measure(View.java:18797)
     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
     at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
     at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2658)
     at android.view.View.measure(View.java:18797)
     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
     at android.view.Choreographer.doCallbacks(Choreographer.java:670)
     at android.view.Choreographer.doFrame(Choreographer.java:606)
     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
  at an...
  </code></pre>
  
  <p>The fragment that contains the tabhost and the viewpager is the next:</p>
  
  <pre><code>public class AdministrarFragment extends Fragment {
  
      public static final String TAG = "AdministrarFragment";
      private FragmentIterationListener mCallback = null;
      public interface FragmentIterationListener{
          public void onFragmentIteration(Bundle parameters);
      }
  
      public static AdministrarFragment newInstance(Bundle arguments){
          AdministrarFragment f = new AdministrarFragment();
          if(arguments != null){
              f.setArguments(arguments);
          }
          return f;
      }
  
      public AdministrarFragment(){
  
      }
  
      //El fragment se ha adjuntado al Activity
      @Override
      public void onAttach(Context context) {
          super.onAttach(context);
  
          Activity activity = context instanceof Activity ? (Activity) context : null;
      }
  
      //El Fragment ha sido quitado de su Activity y ya no está disponible
      @Override
      public void onDetach() {
          super.onDetach();
      }
  
      //La vista ha sido creada y cualquier configuración guardada está cargada
      @Override
      public void onViewStateRestored(Bundle savedInstanceState) {
          super.onViewStateRestored(savedInstanceState);
      }
  
      //El Activity que contiene el Fragment ha terminado su creación
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
      }
  
      //El fragment se ha adjuntado al Activity
      @Override
      public void onAttach(Activity activity) {
          super.onAttach(activity);
  
          mCallback = (FragmentIterationListener) activity;
  
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_administrar, container, false);
  
          TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
          tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.tiempo)));
          tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.gastos)));
          tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
  
          final ViewPager viewPager = (ViewPager) view.findViewById(R.id.pager);
          final AdministrarPageAdapter adapter = new AdministrarPageAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
          viewPager.setAdapter(adapter);
          viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
          tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
                  viewPager.setCurrentItem(tab.getPosition());
              }
  
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
  
              }
  
              @Override
              public void onTabReselected(TabLayout.Tab tab) {
  
              }
          });
  
          return view;
      }
  }
  </code></pre>
  
  <p>And this is my pageadapter:</p>
  
  <pre><code>public class AdministrarPageAdapter extends FragmentStatePagerAdapter {
      int mNumOfTabs;
  
      public AdministrarPageAdapter(FragmentManager fm, int NumOfTabs) {
          super(fm);
          this.mNumOfTabs = NumOfTabs;
      }
  
      @Override
      public Fragment getItem(int position) {
  
          switch (position) {
              case 0:
                  AdministrarTiemposFragment tab_tiempos = new AdministrarTiemposFragment();
                  return tab_tiempos;
              /*case 1:
                  TabFragment2 tab2 = new TabFragment2();
                  return tab2;
              case 2:
                  TabFragment3 tab3 = new TabFragment3();
                  return tab3;*/
              default:
                  return null;
          }
      }
  
      @Override
      public int getCount() {
          return mNumOfTabs;
      }
  }
  </code></pre>