我的片段类出现了错误,即
java.lang.NullPointerException:尝试在fragment.HomeFragment.onResume上的空对象引用上调用虚拟方法'boolean com.google.android.gms.common.api.GoogleApiClient.isConnected()'
这是我在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();
}
我也实施了
有人可以帮我这个吗?
答案 0 :(得分:0)
您需要保留对您正在构建的GoogleApiClient
实例的引用。所以它应该是:
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();
}