我正试图将GoogleApiClient的两个不同对象用于片段中的不同目的。一种用于Google Places Api,另一种用于Locations Api。抛出错误, GoogleApiClient不能为空。由于我已经使用 LocationServices.Api 初始化了第二个googleApiClient,因此出现了问题。
mGoogleApiClient2中出现错误
代码:
private void init() {
Log.d(TAG, "init: initiazling");
if(mGoogleApiClient != null) {
mGoogleApiClient = new GoogleApiClient
.Builder(getActivity())
.addApi(Places.GEO_DATA_API)
.addApi(Places.PLACE_DETECTION_API)
.enableAutoManage(getActivity(), this)
.build();
}
if(mGoogleApiClient2 != null) {
mGoogleApiClient2 = new GoogleApiClient
.Builder(getActivity())
.addApi(LocationServices.API)
.enableAutoManage(getActivity(), this)
.build();
}
}
onPause方法:
@Override
public void onPause() {
super.onPause();
if (mGoogleApiClient != null) {
mGoogleApiClient.stopAutoManage(getActivity());
mGoogleApiClient.disconnect();
}
if (mGoogleApiClient2 != null) {
mGoogleApiClient2.stopAutoManage(getActivity());
mGoogleApiClient2.disconnect();
}
}
onConnectionFailedListener:
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.w(TAG, "onConnectionFailed()");
}