If you take a look on the logcat, the error stated that you have imported wrong LocationListener. Check on your imported library at the top part of the class, it supposed to be com.google.android.gms.location.LocationListener not android.location.LocationListener.

Answer from Amad Yus on Stack Overflow
🌐
Android Developers
developer.android.com › api reference › locationlistener
LocationListener | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Stack Overflow
stackoverflow.com › questions › 63902781 › cannot-be-cast-to-android-location-locationlistener-in-android
gps - cannot be cast to android.location.LocationListener in android - Stack Overflow
getting Cast 4th parameter to 'android.location.LocationListener' in android. ... mLocService.requestLocationUpdates(mLocProvider.getName(), getGpsUpdatePeriod(prefs), getGpsDistanceUpdatePeriod(prefs), (android.location.LocationListener) this); ...
🌐
Stack Overflow
stackoverflow.com › questions › 42352359 › i-am-getting-class-cannot-be-cast-to-android-location-locationlistener
java - I am getting class cannot be cast to android.location.LocationListener - Stack Overflow
Copy import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.Status; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.places.Place; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; public class TabbedActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleA
🌐
Google
developers.google.com › google play services › locationlistener
LocationListener | Google Play services | Google for Developers
October 31, 2024 - LocationListener is an interface for receiving locations from the FusedLocationProviderClient · It is easier to implement and use compared to LocationCallback for most simple location use cases
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 66214289 › java-lang-classcastexception-com-example-bellashbg-locationservice-cannot-be-ca
java.lang.ClassCastException: com.example.BellasHBG.LocationService cannot be cast to com.google.android.gms.location.LocationListener - Stack Overflow
Latest error message: java.lang.RuntimeException: Unable to instantiate service com.example.BellasHBG.LocationService: java.lang.InstantiationException: class com.example.BellasHBG.LocationService cannot be instantiated 2021-02-15T20:34:48.693Z+00:00 ... I removed 'abstract' from the class definition and along with using the import for com.google.android.gms.location.LocationListener and now it seems to be working.
🌐
Wekeepcoding
java.wekeepcoding.com › article › 10449584 › I+am+getting+class+cannot+be+cast+to+android.location.LocationListener
I am getting class cannot be cast to android.location.LocationListener - java
But still no luck. import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.common.api.Status; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices; import com.google.android.gms.location.places.Place; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; public class TabbedActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCall
Top answer
1 of 4
1

Try This class for location

import com.arco.util.location.GPSTracker;
import com.arco.util.location.LocationUtils;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

public class MYActivity extends Activity implements OnClickListener, GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {

    // Global variable to hold the current location
    Location mCurrentLocation;
    // Request to connect to Location Services
    private LocationRequest mLocationRequest;
    // Stores the current instantiation of the location client in this object
    private LocationClient mLocationClient;

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

        // Create a new global location parameters object
        mLocationRequest = LocationRequest.create();
        // Set the update interval
        mLocationRequest.setInterval(LocationUtils.UPDATE_INTERVAL_IN_MILLISECONDS);
        // Use high accuracy
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        // Set the interval ceiling to one minute
        mLocationRequest.setFastestInterval(LocationUtils.FAST_INTERVAL_CEILING_IN_MILLISECONDS);
        mLocationClient = new LocationClient(this, this, this);




    } // End OnCreate()



    @Override
    public void onStart() {
        super.onStart();
        mLocationClient.connect();
    }

    @Override
    public void onStop() {
        mLocationClient.disconnect();
        super.onStop();
    }

    /**
     * Verify that Google Play services is available before making a request.
     */
    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) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult mConnectionResult) {

        if (mConnectionResult.hasResolution()) {
            try {
                // Start an Activity that tries to resolve the error
                mConnectionResult.startResolutionForResult(this, LocationUtils.CONNECTION_FAILURE_RESOLUTION_REQUEST);
            } catch (IntentSender.SendIntentException e) {
                e.printStackTrace();
            }
        } 
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        try {
            if (servicesConnected()) {
                mLocationClient.requestLocationUpdates(mLocationRequest, this);
                // Get the current location
                Location currentLocation = mLocationClient.getLastLocation();
                if (currentLocation != null) {
                    mCurrentLocation = currentLocation;
                }
               mCurrentLocation.getLatitude();
               mCurrentLocation.getLongitude()
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDisconnected() {
    }

    @Override
    public void onLocationChanged(Location location) {
        mCurrentLocation = location;
    }
}
2 of 4
1

@Moradiya Akash, @Karan Mavadhiya, @pratt : Thankyou so much for helping. I was trying to display the contents of location as charequence in the line

loc.setText((CharSequence) location);

Once I removed it, the app works fine. Thanks Vny Kumar for helping me debug the code myself, it would help me in a long run. :)

🌐
Stack Overflow
stackoverflow.com › questions › 44475241 › java-lang-classcastexception-while-adding-permission-in-service-class-to-fetch-l
android - java.lang.ClassCastException while Adding Permission in Service class to Fetch Location - Stack Overflow
public class MyService extends Service { private static final String TAG = "BOOMBOOMTESTGPS"; private LocationManager mLocationManager = null; private static final int LOCATION_INTERVAL = 1000; private static final float LOCATION_DISTANCE = 10f; private final IBinder mBinder = new LocalBinder(); private Intent intent; Location mLastLocation; public class LocalBinder extends Binder { public MyService getServerInstance() { return MyService.this; } } public Location getLocation(){ return mLastLocation; } private class LocationListener implements android.location.LocationListener { public Location
🌐
ExceptionsHub
exceptionshub.com › cannot-be-cast-to-com-google-android-gms-location-locationlistener.html
Cannot be cast to com.google.android.gms.location.LocationListener - ExceptionsHub
May 16, 2018 - 05-16 17:22:12.849 17072-17072/com.plusequalsto.ride E/AndroidRuntime: FATAL EXCEPTION: main Process: com.plusequalsto.ride, PID: 17072 java.lang.ClassCastException: com.plusequalsto.ride.MainActivity cannot be cast to com.google.android.gms.location.LocationListener at com.plusequalsto.ri...