问题
I am working with the Google Play LocationClient. I have initialized it in onCreate() as stated in the docs:
mLocationClient = new LocationClient(this, this, this);
and I am doing a connect in onStart()
mLocationClient.connect();
It works fine in my Android phone, but in the Developers Console I see that a NullPointerException is happening in the connect()
line.
How can this happen?
回答1:
That is probably because the device that cause the NPE does not have Google Play Service or is not up to dated. In onCreate you can check
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (errorCode != ConnectionResult.SUCCESS)
{
if (DEBUG) {Log.d(TAG, "errorCode = " + errorCode);}
Dialog errorDialog = GooglePlayServicesUtil.getErrorDialog(errorCode, this,
1, new DialogCancelListener());
errorDialog.show();
}
Which will show a dialog to ask the user to go to Google Store to update or install.
In onStart you need to check for null
if (mLocationClient != null)
{
mLocationClient.connect();
}
来源:https://stackoverflow.com/questions/18279382/android-googleplay-locationclient-is-null-although-initialized-in-oncreate