1

I'm getting error on my fragment class which is state that

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.common.api.GoogleApiClient.isConnected()' on a null object reference at fragment.HomeFragment.onResume

This is my partial code on HomeFragment

private GoogleApiClient mGoogleApiClient;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mCurrentLocation = savedInstanceState.getParcelable(KEY_LOCATION);
        mCameraPosition = 
savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
    }        buildGoogleApiClient();
}

@Override
public void onResume() {
    super.onResume();
    if (mGoogleApiClient.isConnected()) {
        getDeviceLocation();
    }
    updateMarkers();
}

    private synchronized void buildGoogleApiClient() {
    GoogleApiClient.Builder builder = new GoogleApiClient.Builder(getActivity());
    mGoogleApiClient = builder.build();
    //builder.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */);
    builder.addConnectionCallbacks(this);
    builder.addApi(LocationServices.API);
    builder.addApi(Places.GEO_DATA_API);
    builder.addApi(Places.PLACE_DETECTION_API);
    builder.build();
    createLocationRequest();
}

I also implemented

  1. OnMapReadyCallback
  2. GoogleApiClient.ConnectionCallbacks
  3. GoogleApiClient.OnConnectionFailedListener
  4. LocationListener
  5. DirectionFinderListener

Can someone help me with this?

nao
  • 33
  • 1
  • 9
  • Possible duplicate of [What is NullPointerException and how do I fix it](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it). There is only one relevant code part of two we need: you never showed us where did you assign `mGoogleApiAgent`. – M. Prokhorov Apr 09 '17 at 13:42
  • @M.Prokhorov I had edited my post. – nao Apr 09 '17 at 14:01
  • Considering the part you've added, my link is even more relevant than it was. – M. Prokhorov Apr 09 '17 at 14:03

1 Answers1

1

You need to keep a reference to the instance of GoogleApiClient you're building. So it should be:

private void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
        .addConnectionCallbacks(this)
        .addApi(LocationServices.API)
        .addApi(Places.GEO_DATA_API)
        .addApi(Places.PLACE_DETECTION_API)
        .build();
    createLocationRequest();
}
quiro
  • 357
  • 1
  • 8
  • is it on private synchronized void buildGoogleApiClient()? – nao Apr 09 '17 at 13:41
  • yes. Also you don't even need to use synchronized if you only call the `buildGoogleApiClient()` method in the `onCreate`. It's always going to get called by the main thread, so no synchronization issue – quiro Apr 09 '17 at 13:43
  • I did that but now I'm getting error stated that >must call addApi() to add at least one API – nao Apr 09 '17 at 13:57
  • @nao, you should add at least one Api then - exception messages are usually at least somewhat helpful. – M. Prokhorov Apr 09 '17 at 14:03
  • @nao I edited my answer with the correct way of building the `GoogleApiClient` – quiro Apr 09 '17 at 14:06
  • @quiro Thank you for your answer :) – nao Apr 09 '17 at 14:13