2013-05-31 41 views
16

我正在回收提供的代码,以便在从http://developer.android.com/training/location/retrieve-current.html获取位置数据之前检查用户的设备是否具有Google Play服务。当复制粘贴到我IDE,Eclipse中正确地指出了线的错误,因为“connectionResult”从来没有被定义的,也不是“getSupportFragmentManager”不明确的Android当前位置检索教程

int errorCode = connectionResult.getErrorCode(); 

errorFragment.show(getSupportFragmentManager(), 
        "Location Updates"); 

我应该创建上面的一个变量叫做ConnectionResult connectionResult来解决这个问题?我不知道如何纠正第二个问题。

此外,该行

mLocationClient = new LocationClient(this, this, this); 

进一步沿着页面建议在MainActivity类不满足LocationClient构造,唤起另一个错误推杆。

更新:本教程的另一个问题。大家好,本教程引用它尚未在此创建的类LocationResult:http://developer.android.com/training/location/receive-location-updates.html。我应该如何/在哪里定义这个?

+1

你的标题是不是与你的描述 – juned

+0

@juned谢谢你,我希望现在更清楚 – NumenorForLife

回答

35

本教程具有误导性。如果你想检查谷歌播放服务存在执行以下操作。

int errorCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 
if (errorCode != ConnectionResult.SUCCESS) { 
    GooglePlayServicesUtil.getErrorDialog(errorCode, this, 0).show(); 
} 

这会自动显示一个适当的错误对话框,如果它不存在。

你的第二个问题。本教程的其余部分确实需要遵循。你需要实现GooglePlayServicesClient.ConnectionCallbacksGooglePlayServicesClient.OnConnectionFailedListener如果你想创建使用new LocationClient(this, this, this);

注意locationclient:不要尝试使用locationclient直到onConnected方法被调用在回调之后。

-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