Android GooglePlay LocationClient is null, although initialized in onCreate()

南楼画角 提交于 2019-12-12 00:44:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!