2013-05-31 42 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