title: NullPointerException in FrameLayout.onMeasure()
body: <p><em>Edit: As I've debugged this and tried various changes, I realized the original code I posted wasn't really relevant, so I removed it:</em></p>
  
  <p>I have an <code>Activity</code> that can switch between 3 different <code>Fragment</code>s when the user pushes one of 3 <code>IconButton</code>s I have inserted into the ActionBar in a custom layout, like switching between various modes:</p>
  
  <p><img src="https://i.stack.imgur.com/PLTwI.png" alt="enter image description here"></p>
  
  <p>When I first launch the app (having uninstalled it), I'm defaulted to the Globe mode. The Globe mode has several fragments in a <code>ViewPager</code>, most of these fragments are subclasses of the <code>android.support.v4.app.ListFragment</code>. I am creating all 8 of my tabs in the main Activity's onCreate method. This works as expected, and everything loads properly.</p>
  
  <p>When I go to the "Profile" mode (by clicking on the Face iconbutton), I can log into the app, and see my profile. This also works as expected, everything is loading properly.</p>
  
  <p>When I <em>push the APK again</em> from Android Studio, since I am now logged in (saved Preferences), it defaults me to the "friend feed" mode (the two people holding hands). I can switch modes to the Profile, and everything works as expected, but when I switch to the Globe mode, it is crashing in Android code:</p>
  
  <pre><code>08-14 14:43:22.744  28804-29323/com.trover E/AndroidRuntime﹕ FATAL EXCEPTION: main
      Process: com.trover, PID: 28804
      java.lang.NullPointerException
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:309)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
              at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
              at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
              at android.view.View.measure(View.java:16497)
              at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1912)
              at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1109)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1291)
              at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
              at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
              at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
              at android.view.Choreographer.doCallbacks(Choreographer.java:574)
              at android.view.Choreographer.doFrame(Choreographer.java:544)
              at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
              at android.os.Handler.handleCallback(Handler.java:733)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:136)
              at android.app.ActivityThread.main(ActivityThread.java:5001)
              at java.lang.reflect.Method.invokeNative(Native Method)
              at java.lang.reflect.Method.invoke(Method.java:515)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
              at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>In the past two weeks I have made two major changes to the project:</p>
  
  <ol>
  <li>I have removed the ActionbarSherlock library. I originally was hoping to remove the support.v4 library as well until I realized I needed it for the ViewPager.</li>
  <li>I switched from Eclipse to Android Studio, which included moving all of the files to the new Android Studio structure.</li>
  </ol>
  
  <p>This morning I re-installed Eclipse and went to the point right after removing ActionbarSherlock, and could not recreate this crash.</p>
  
  <p>Things I have tried:</p>
  
  <ul>
  <li>Manually building the project with Gradle, and manually installing it and running it. When I do the same steps, it crashes in the same way.</li>
  <li><p>Running this through the debugger, and stopping at the crash. Line 309 is the following:</p>
  
  <pre><code>    if (mMeasureAllChildren || child.getVisibility() != GONE) {
  </code></pre></li>
  </ul>
  
  <p>At this point in the code, <code>child</code> is <code>null</code>, however, the call to <code>getChildCount()</code> on line 296 is returning a value of 2. <em>I still have no idea which View is actually crashing at this point</em>, there is no defining value in any of the variable as I browse through them in the debugger.</p>
  
  <ul>
  <li>When you log in/out, items that are in the overflow menu are changed. When you click on the <code>IconButton</code>s on the top, I call <code>setBackgroundDrawable</code> on the IconButtons to set the yellow highlight (or set <code>null</code> if it's not the active button anymore). When I comment out both the code in <code>onCreateOptionsMenu</code> and the code in <code>setHighlightedIconButton()</code>, the crash no longer occurs</li>
  <li>When I remove all of the fragments from the <code>ViewPager</code>, the crash no longer occurs</li>
  <li>As @kcoppock mentioned below, it could likely be that I'm improperly inflating something somewhere. Following the article he linked, I modified every call in my app where I was calling <code>inflater.inflate(R.layout.somelayout, null)</code> instead to <code>inflater.inflate(R.layout.somelayout, viewGroupContainer, false)</code>, except in the case where I am creating custom views on the tabs (where I don't know the viewGroup to parent them under). In that case I'm manually setting LayoutParameters.</li>
  </ul>
  
  <p>I'm completely at a loss at this point as to what to even try next.</p>
  
  <p><em>update:</em> Changing the app to <em>always</em> start in the Globe Mode prevents the crash, so it seems to be something with the switch to the Globe Mode if we didn't start with it active.</p>
  
  <p>Here is my code to build the Tabs for the main Activity's viewPager:</p>
  
  <pre><code>    // We HAVE to read this value out before creating the layout, or onTabSelected will set it back to zero
      final SharedPreferences prefs = getApplicationContext().getSharedPreferences(
              Const.Preferences.PREFS_FILE, Context.MODE_PRIVATE);
      boolean deniedLocationServices = prefs.getBoolean(Const.Preferences.LOCATION_SERVICES_DENIED, false);
      int previousTab = prefs.getInt(Const.Preferences.PREVIOUS_TAB, 0);
  
      setContentView(R.layout.main_browse);
  
      mFragmentManager = getSupportFragmentManager();
  
      mActionBar = getActionBar();
      mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
      // these three lines hide the 'Trover' logo
      mActionBar.setDisplayHomeAsUpEnabled(false);
      mActionBar.setDisplayUseLogoEnabled(false);
      mActionBar.setIcon(new ColorDrawable(android.R.color.black));
      mActionBar.setCustomView(R.layout.action_bar_icons);
      mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME);
      final int numTabs = 8;
  
      TabbedBrowsePagerTab[] tabs = new TabbedBrowsePagerTab[numTabs];
  
      tabs[mAllTabPosition] = buildAllTab();
      tabs[mWhatsHotTabPosition] = buildWhatsHotTab();
      tabs[mWhatsNewTabPosition] = buildWhatsNewTab();
      tabs[mJumpToTabPosition] = buildJumpToTab();
      tabs[mFoodTabPosition] = buildFoodTab();
      tabs[mOutdoorTabPosition] = buildOutdoorTab();
      tabs[mArtTabPosition] = buildArtTab();
      tabs[mLatestTabPosition] = buildLatestTab();
  
      mPagerAdapter = new TabbedBrowsePagerAdapter(this, tabs);
      mViewPager = (ViewPager) findViewById(R.id.pager);
      mViewPager.setAdapter(mPagerAdapter);
  
      mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
          @Override
          public void onPageSelected(final int position) {
              try {
                  mActionBar.setSelectedNavigationItem(position);
              } catch (final IllegalStateException e) {
                  TroverApplication.logError(TAG, "IllegalArgumentsException setting tab position in PageChangeListener");
              }
          }
      });
  
      // Build the actual tabs on the actionbar
      for (int i = 0; i &lt; tabs.length; i++) {
          mActionBar.addTab(mActionBar.newTab()
                  .setTabListener(this));
  
          LayoutInflater inflater = LayoutInflater.from(this);
          View customView = inflater.inflate(R.layout.custom_action_bar_tabs, null);
          customView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
          TextView tabCustom = (TextView) customView.findViewById(R.id.custom_action_bar_tab);
          tabCustom.setText(tabs[i].getTabTitle());
          tabCustom.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
          tabCustom.setTypeface(TroverApplication.getDefaultFontBold());
          mActionBar.getTabAt(i).setCustomView(tabCustom);
      }
  
      // Now set up the button listeners for those icons
      mActionBar.getCustomView().findViewById(R.id.action_bar_friend_feed_button).setOnClickListener(this);
      mActionBar.getCustomView().findViewById(R.id.action_bar_me_button).setOnClickListener(this);
      mActionBar.getCustomView().findViewById(R.id.action_bar_nearby_button).setOnClickListener(this);
  
      mViewPager.setCurrentItem(previousTab);
  
      TroverLocationManager manger = TroverLocationManager.get();
      if (!manger.isNetworkLocationEnabled() &amp;&amp; !deniedLocationServices) {
          showLocationServicesDialog();
      }
  
      if (AuthManager.get().isAuthenticated()) {
          mCurrentMode = Mode.NEWS_MODE; // &lt;----- if I change this to GLOBE_MODE it doesn't crash
      } else {
          mCurrentMode = Mode.GLOBE_MODE;
      }
      validateView();
  </code></pre>
  
  <p>Here is the main_browse.xml layout:</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_height="fill_parent"
      android:layout_width="fill_parent" &gt;
  
      &lt;android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;Button
          android:id="@+id/main_camera_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_alignParentRight="true"
          android:layout_marginBottom="15dp"
          android:layout_marginRight="15dp"
          android:background="@drawable/camera_floating" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is the setHighlightedIconButton function:</p>
  
  <pre><code>private void setHighlightedIconButton() {
      ImageButton button;
      View iconContainer = mActionBar.getCustomView();
  
      PaintDrawable selectedBackground = new PaintDrawable(getResources().getColor(R.color.trover_selected_icon_background));
  
      button = (ImageButton) iconContainer.findViewById(R.id.action_bar_friend_feed_button);
      if (mCurrentMode == Mode.NEWS_MODE) {
          button.setBackgroundDrawable(selectedBackground);
          button.setClickable(false);
      } else {
          button.setBackgroundDrawable(null);
          button.setClickable(true);
      }
  
      button = (ImageButton) iconContainer.findViewById(R.id.action_bar_nearby_button);
      if (mCurrentMode == Mode.GLOBE_MODE) {
          button.setBackgroundDrawable(selectedBackground);
          button.setClickable(false);
      } else {
          button.setBackgroundDrawable(null);
          button.setClickable(true);
      }
  
      button = (ImageButton) iconContainer.findViewById(R.id.action_bar_me_button);
      if (mCurrentMode == Mode.PROFILE_MODE) {
          button.setBackgroundDrawable(selectedBackground);
          button.setClickable(false);
      } else {
          button.setBackgroundDrawable(null);
          button.setClickable(true);
      }
  }
  </code></pre>
  
  <p>And this is the validateView() function which calls it:</p>
  
  <pre><code>/**
   * Handles transitions between different fragments by checking the current mode and authentication state.
   * This function can handle being called multiple times, and will always try to do the least work possible
   * each time.
   */
  private void validateView() {
      invalidateOptionsMenu();
      setHighlightedIconButton();
  
      boolean authenticated = AuthManager.get().isAuthenticated();
  
      switch(mCurrentMode) {
          case GLOBE_MODE:
              // Note - we don't record a screen here because the tabs will do that
              removeMeFragment();
              removeNewsFragment();
              removeOnboardingFragment();
              mViewPager.setVisibility(View.VISIBLE);
              mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
              mPagerAdapter.madeVisible();
              break;
          case NEWS_MODE:
              if (mPreviousTabPosition == mJumpToTabPosition) {
                  InputMethodManager imm = (InputMethodManager) getSystemService(
                          Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);
              }
              recordScreen(getCurrentModeTrackingString(), this);
              removeMeFragment();
              removeOnboardingFragment();
              if (mNewsFeedFragment == null) {
                  mNewsFeedFragment = DiscoveryListFragment.newNewsFeedInstance();
                  mFragmentManager.beginTransaction().add(android.R.id.content, mNewsFeedFragment).commit();
              }
              mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
              mViewPager.setVisibility(View.GONE);
              break;
          case PROFILE_MODE:
              if (mPreviousTabPosition == mJumpToTabPosition) {
                  InputMethodManager imm = (InputMethodManager) getSystemService(
                          Context.INPUT_METHOD_SERVICE);
                  imm.hideSoftInputFromWindow(mViewPager.getWindowToken(), 0);
              }
              if (authenticated) {
                  recordScreen(getCurrentModeTrackingString(), this);
                  removeNewsFragment();
                  removeOnboardingFragment();
                  if (mProfileFragment == null) {
                      mProfileFragment = UserDetailFragment.newMeInstance();
                      mFragmentManager.beginTransaction().add(android.R.id.content, mProfileFragment).commit();
                  }
              } else {
                  recordScreen(getCurrentModeTrackingString() + "/onboarding", this);
                  removeMeFragment();
                  removeNewsFragment();
                  if (mOnboardingFragment == null) {
                      removeOnboardingFragment();
                      mOnboardingFragment = new OnboardingFragment();
                      mFragmentManager.beginTransaction().add(android.R.id.content, mOnboardingFragment).commit();
                  }
              }
              mViewPager.setVisibility(View.GONE);
              break;
          default:
              TroverApplication.logError(TAG, "Invalid mode!");
      }
  }
  </code></pre>
  
  <p>Here is the <code>onCreateOptionsMenu</code> for the main Activity:</p>
  
  <pre><code>public boolean onCreateOptionsMenu(final Menu menu) {
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.menu, menu);
      if (!AuthManager.get().isAuthenticated()) {
          MenuItem item = menu.findItem(R.id.menu_notifications);
          if (item != null) {
              item.setVisible(false);
          }
          item = menu.findItem(R.id.menu_recommended_users);
          if (item != null) {
              item.setVisible(false);
          }
      }
      return true;
  }
  </code></pre>
  
  <p>This is the part of the <code>FrameLayout.onMeasure()</code> function that is part of the Android API 19 source code where it is crashing:</p>
  
  <pre><code>@Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
      int count = getChildCount();  &lt;-- this is returning 2
  
      final boolean measureMatchParentChildren =
              MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
              MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
      mMatchParentChildren.clear();
  
      int maxHeight = 0;
      int maxWidth = 0;
      int childState = 0;
  
      for (int i = 0; i &lt; count; i++) {
          final View child = getChildAt(i);
          if (mMeasureAllChildren || child.getVisibility() != GONE) { &lt;-- crash happens here
              measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
              final LayoutParams lp = (LayoutParams) child.getLayoutParams();
              maxWidth = Math.max(maxWidth,
                      child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
              maxHeight = Math.max(maxHeight,
                      child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
              childState = combineMeasuredStates(childState, child.getMeasuredState());
              if (measureMatchParentChildren) {
                  if (lp.width == LayoutParams.MATCH_PARENT ||
                          lp.height == LayoutParams.MATCH_PARENT) {
                      mMatchParentChildren.add(child);
                  }
              }
          }
      }
  </code></pre>