cannot be cast to android.location.LocationListener in android

588 Views Asked by At

I am trying access GPS in my application. I have given the permission but still getting error.

If I will import instead of import com.google.android.gms.location.LocationListener to import android.location.LocationListener;

and

mLocService.requestLocationUpdates(mLocProvider.getName(), getGpsUpdatePeriod(prefs), getGpsDistanceUpdatePeriod(prefs), (android.location.LocationListener) this);

code is comping but it not in entering into if() condition and "gps permission--->2" not getting in log.

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


public class MainActivity extends RoboActivity implements ObdProgressListener, LocationListener, GpsStatus.Listener {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gpsStart();
}

 private synchronized void gpsStart() {
    if (!mGpsIsStarted && mLocProvider != null && mLocService != null && mLocService.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        Log.d(TAG, "gps permission--->1");
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
           Log.d(TAG, "gps permission--->2");
            mLocService.requestLocationUpdates(mLocProvider.getName(), getGpsUpdatePeriod(prefs), getGpsDistanceUpdatePeriod(prefs), (com.google.android.gms.location.LocationListener) this);
            mGpsIsStarted = true;
            return;
        }
 } else {
        Log.d(TAG, "gps permission--->3");
        gpsStatusTextView.setText(getString(R.string.status_gps_no_support));
    }
}



 

getting Cast 4th parameter to 'android.location.LocationListener' in android.

If I give like below ,

mLocService.requestLocationUpdates(mLocProvider.getName(), getGpsUpdatePeriod(prefs), getGpsDistanceUpdatePeriod(prefs), (android.location.LocationListener) this);

getting error ** cannot be cast to android.location.LocationListener**

1

There are 1 best solutions below

1
Ridcully On

Seems that requestLocationUpdates() expects a android.location.LocationListener as 4th parameter, but you are passing it a com.google.android.gms.location.LocationListener which, although it has the same name (except for the package) is a completely different class.

So basically, its like passing an Integer when a String is expected.