2013-05-31 10 views
16

Ricicla il codice fornito per verificare che il dispositivo dell'utente disponga di servizi Google Play su di esso prima di acquisire i dati sulla posizione da http://developer.android.com/training/location/retrieve-current.html. Quando copia-incolla nel mio IDE, Eclipse giustamente rileva errori nelle linee, perché "connectionResult" non è mai stato definito, né è "getSupportFragmentManager"Esercitazione di recupero posizione corrente Android non leggibile

int errorCode = connectionResult.getErrorCode(); 

e

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

Devo solo creare una variabile sopra chiamata ConnectionResult connectionResult per risolvere il problema? Non sono sicuro di come correggere il secondo.

Inoltre, la linea

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

da ulteriori lungo la pagina suggerisce mettendo in classe MainActivity che non soddisfa il costruttore LocationClient, evocando un altro errore.

Aggiornamento: un altro problema con il tutorial. Ciao a tutti, il tutorial fa riferimento alla classe LocationResult che non ha creato qui: http://developer.android.com/training/location/receive-location-updates.html. Come/dove dovrei definire questo?

+1

il titolo non è correlato alla descrizione – juned

+0

@juned grazie, spero che sia più chiaro ora – NumenorForLife

risposta

35

Il tutorial è fuorviante. Se vuoi controllare i servizi di google play, fai quanto segue.

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

Questo mostrerà automaticamente una finestra di errore appropriato se non lo fa a esistere.

Al tuo secondo problema. Il resto del tutorial deve essere seguito. Si ha bisogno di implementare GooglePlayServicesClient.ConnectionCallbacks e GooglePlayServicesClient.OnConnectionFailedListener se si desidera creare il il locationclient utilizzando new LocationClient(this, this, this);

Nota: non tentare di utilizzare la locationclient fino a dopo il metodo onConnected viene chiamato nel callback.

-1

Seguendo il tutorial mi sono imbattuto negli stessi errori, tuttavia il codice di esempio fornito sembra essere correttamente implementato.

/** 
* 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

Problemi correlati