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
Can someone help me with this?
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.