2015-02-13 12 views
6

Ho creato una mappa basata su Android posizione tuttavia quando provo e chiamo: LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);LocationServices.FusedLocationApi.removeLocationUpdates (mGoogleApiClient, this); Cerca di cast come com.google.android.gms.location.LocationListener)

Android in studio cerca di avere me gettato come

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this); 

Ciò tuttavia causa l'arresto anomalo dell'app quando viene chiamato.

ho il seguente codice di posizione per la mia app:

import android.content.IntentSender; 
import android.location.Location; 
import android.location.LocationListener; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.util.Log; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.android.gms.location.LocationServices; 

import java.util.List; 

import model.SiteModel; 
import model.SqlHelper; 

public class FindNearestLocationMap extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { 

    private GoogleMap mMap; // Might be null if Google Play services APK is not available. 

    private GoogleApiClient mGoogleApiClient; 

    public static final String TAG = FindNearestLocationMap.class.getSimpleName(); 

    private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000; 

    private LocationRequest mLocationRequest; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_find_nearest_location_map); 

     mGoogleApiClient = new GoogleApiClient.Builder(this) 
       .addConnectionCallbacks(this) 
       .addOnConnectionFailedListener(this) 
       .addApi(LocationServices.API) 
       .build(); 

     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY) 
       .setInterval(10 * 1000)  // 10 seconds, in milliseconds 
       .setFastestInterval(1 * 1000); // 1 second, in milliseconds 

     setUpMapIfNeeded(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     setUpMapIfNeeded(); 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onPause() { //////ERROR IS HERE//////// 
     super.onPause(); 
     if (mGoogleApiClient.isConnected()) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (com.google.android.gms.location.LocationListener) this); 
      mGoogleApiClient.disconnect(); 
     } 
    } 
.... 

Ho tutti i metodi implementati che le classi richiedono e sono in grado di eseguire il programma. Si rompe solo quando onPause viene chiamato qualche idea su cosa dovrei fare?

Grazie.

risposta

16

Si sta utilizzando l'importazione errata per LocationListener. Hai:

import android.location.LocationListener; 

che è l'eredità LocationListener e non ascolta la posizione del Provider fusa:

import com.google.android.gms.location.LocationListener;