title: Android - Google Maps NullPointer in onResume
body: <p>I am having some random crashes in <code>super.onResume</code> with Google maps. It only happens in debug mode, not in production. It is weird how the map is added to the fragment (not coded by myself), but it was working properly before. Just some day started to crash. Below you can see the error and the code. I would really appreciate some help!</p>
  
  <pre><code>Fatal Exception: java.lang.NullPointerException
  com.google.maps.api.android.lib6.e.y.b (Unknown Source)
  com.google.android.gms.maps.MapFragment.onResume (Unknown Source)
  com.xxxx.xxxx.Fragments.FragmentMap.onResume (FragmentMap.java:983)
  com.xxxx.xxxx.Fragments.FragmentMap.onConnected(FragmentMap.java:2667)
  com.google.android.gms.common.internal.zzk.zzk (Unknown Source)
  dalvik.system.NativeStart.main (NativeStart.java)
  </code></pre>
  
  <p>And this is the Fragment where is painting the Map:</p>
  
  <pre><code>public class FragmentMap extends MapFragment implements LocationListener,
      GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, ResultCallback&lt;LocationSettingsResult&gt; {
  GoogleMap map;
  GoogleApiClient 
  {...}
  
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
  //Instantiate here all not view objects (Fragments good practices)
      super.onCreate(savedInstanceState);
      setRetainInstance(true);
      if (checkPlayServices()) {
  
          // Building the GoogleApi client
          buildGoogleApiClient();
          createLocationRequest();
          buildLocationSettingsRequest();
          checkLocationSettings();
  
      }
  }
  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      map = this.getMap();
      map.getUiSettings().setZoomControlsEnabled(false);
      map.getUiSettings().setMyLocationButtonEnabled(false);
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View mapView = super.onCreateView(inflater, container, savedInstanceState);
      RelativeLayout rl = new RelativeLayout(getActivity());
  
      if (condition1) {
          View firstOverlay = inflater.inflate(R.layout.first_map_layout, null);
          rl.addView(mapView);
          rl.addView(firstOverlay);
          setHasOptionsMenu(true);
  
      } else {
          View srcvAddress = inflater.inflate(R.layout.address_finder, null);
          rl.addView(mapView);
          rl.addView(srcvAddress);
      }
  
      return rl;
  }
  
  @Override
  public void onResume() {
      super.onResume(); &lt;----------------------- **HERE CRASHES!!**
      stopUpdatePositionService();
      if (mGoogleApiClient.isConnected()) {
         {...}
      }
  }
  
  /**
   * Creating google api client object
   */
  private synchronized void buildGoogleApiClient() {
      mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(LocationServices.API).build();
  }
  
   /**
   * Stopping location updates
   */
  private void stopLocationUpdates() {
      if (mGoogleApiClient.isConnected())
          LocationServices.FusedLocationApi.removeLocationUpdates(
                  mGoogleApiClient, this);
  }
      @Override
  public void onStart() {
      super.onStart();
      if (mGoogleApiClient != null) {
          mGoogleApiClient.connect();
      }
  }
  
  /**
   * Google api callback methods
   */
  @Override
  public void onConnectionFailed(@NonNull ConnectionResult result) {
      Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "
              + result.getErrorCode());
  }
  
  @Override
  public void onConnected(Bundle arg0) {
  
      // Once connected with google api, get the location
      onResume();
      //displayLocation();
  }
  
  @Override
  public void onConnectionSuspended(int arg0) {
      mGoogleApiClient.connect();
  }
  
  /**
   * Creating location request object
   */
  private void createLocationRequest() {
      mLocationRequest = new LocationRequest();
      mLocationRequest.setInterval(UPDATE_INTERVAL);
      mLocationRequest.setFastestInterval(FATEST_INTERVAL);
      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
      mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
  }
  </code></pre>
  
  <p>Thanks in advance!</p>