我正在回收所提供的代码,以便在从http://developer.android.com/training/location/retrieve-current.html获取位置数据之前检查用户的设备上是否有Google Play服务。当将其复制粘贴到我的IDE中时,Eclipse正确地指出了行中的错误,因为“connectionResult”从未定义过,“getSupportFragmentManager”也不是
int errorCode = connectionResult.getErrorCode();
和
errorFragment.show(getSupportFragmentManager(),
"Location Updates");
我是否应该创建一个名为ConnectionResult connectionResult的变量来解决问题?我不确定如何纠正第二个问题。
此外,该行
mLocationClient = new LocationClient(this, this, this);
从页面的另一侧开始建议放入不满足LocationClient构造函数的MainActivity类,从而引发另一个错误。
更新:本教程的另一个问题。大家好,本教程引用了尚未在此处创建的类LocationResult:http://developer.android.com/training/location/receive-location-updates.html。我应该如何/在哪里定义这个?
答案 0 :(得分:35)
该教程具有误导性。如果您想检查谷歌播放服务存在,请执行以下操作。
int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (errorCode != ConnectionResult.SUCCESS) {
GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show();
}
如果不存在,将自动显示相应的错误对话框。
你的第二个问题。确实需要遵循本教程的其余部分。如果要使用GooglePlayServicesClient.ConnectionCallbacks
GooglePlayServicesClient.OnConnectionFailedListener
和new LocationClient(this, this, this);
注意:在回调中调用onConnected
方法之前,请勿尝试使用locationclient。
答案 1 :(得分:-1)
在本教程之后我遇到了相同的错误,但提供的代码示例似乎已正确实现。
/**
* Verify that Google Play services is available before making a request.
*
* @return true if Google Play services is available, otherwise false
*/
private boolean servicesConnected() {
// Check that Google Play services is available
int resultCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
// If Google Play services is available
if (ConnectionResult.SUCCESS == resultCode) {
// In debug mode, log the status
Log.d(LocationUtils.APPTAG, getString(R.string.play_services_available));
// Continue
return true;
// Google Play services was not available for some reason
} else {
// Display an error dialog
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0);
if (dialog != null) {
ErrorDialogFragment errorFragment = new ErrorDialogFragment();
errorFragment.setDialog(dialog);
errorFragment.show(getSupportFragmentManager(), LocationUtils.APPTAG);
}
return false;
}
}
http://developer.android.com/shareables/training/LocationUpdates.zip