2

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(); 
}
Rick Smith
  • 9,031
  • 15
  • 81
  • 85
Danfoa
  • 930
  • 2
  • 11
  • 24

2 Answers2

16

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.

Gero
  • 1,842
  • 25
  • 45
  • Thanks!!, that was the problem ;). I still cant vote, (reputation) but im really grateful. – Danfoa Apr 21 '15 at 19:36
  • Even adding com.google.android.gms.location.LocationListener dint work for me...any other suggestions? – Sharath Sep 23 '15 at 11:39
  • @Sharath, you may have missed the dependency "compile 'com.google.android.gms:play-services-location:7.5.0'" on your gradle file – Gero Sep 23 '15 at 15:05
  • @Gero i imported com.google.android.gms.location.LocationListener but it's not working on my fragment still crashing – Anshuman Pattnaik Jul 23 '16 at 07:11
0

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

do

public class DataActivity extends FragmentActivity implements **com.google.android.gms.location.LocationListener**, ConnectionCallbacks , OnConnectionFailedListener, OnMapReadyCallback{

after that you have to implement all its methods