Can't use an activity as an instance of LocationListener when calling requestLocationUpdates()

Danfoa picture Danfoa · Apr 21, 2015 · Viewed 8.9k times · Source

In my App, after connecting to LocationServices.API, in the onConnected() callback, I start a location Updates Request, the problem is that the Activity that handles the process (DataActivity) is not recognize as an instance of LocationListener, it shows an error, and suggest casting.

protected void startLocationUpdates() {
    PendingResult locationUpdatesResult = LocationServices.FusedLocationApi.requestLocationUpdates(locationGoogleApiClient, locationRequest, (com.google.android.gms.location.LocationListener) this);
}

When I cast the last argument to(com.google.android.gms.location.LocationListener) the IDE lets me compile, but an ClassCastException is thrown

java.lang.ClassCastException: com.example.[...].DataActivity cannot be cast to com.google.android.gms.location.LocationListener

I Dont really Know why this happen.

Here is The Declaration of the Class.

public class DataActivity extends FragmentActivity implements LocationListener, ConnectionCallbacks , OnConnectionFailedListener, OnMapReadyCallback{

the OnCreate():

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_data);

    //Create the GoogleLocationAPI reference
    buildGoogleLocationApiClient();

    //Create Location Request
    createLocationRequest();

    //Initialize the mapFragment
    initializeMapFragment();

    updateUI();
}

the Other Methods:

protected synchronized void buildGoogleLocationApiClient(){
    locationGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    locationGoogleApiClient.connect();
    Log.v("**INFO: ", "API CLIENT");
}

protected static void createLocationRequest() {
    locationRequest = new LocationRequest();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(10000);
    locationRequest.setFastestInterval(5000);
    Log.i("**INFO: ", "LocationRequest created");
}

protected void initializeMapFragment(){
    //Initialize the mapFragment
    mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    //When mapFragment is ready...
    mapFragment.getMapAsync(this);
}

So when the connection is made (OnConnected), I call this method, when the error pops.

protected void startLocationUpdates() {
    PendingResult locationUpdatesResult = LocationServices.FusedLocationApi.requestLocationUpdates(locationGoogleApiClient, locationRequest, this);
    locationUpdatesResult.await(); 
}

Answer

Gero picture Gero · Apr 21, 2015

Make sure your DataActivity implements com.google.android.gms.location.LocationListener and not android.location.LocationListener.
You may have imported android.location.LocationListener by mistake.