androidgoogle-mapsgoogle-maps-api-3castinglocationlistener

Unable to cast the main Activity to Location Listener i.e. com.google.android.gms.location.LocationListener


as i am done with my coding and tired to check whats going on with the app on emulator and stuck with an error called java.lang.ClassCastException i.e UserLocation cannot be cast to com.google.android.gms.location.LocationListener

My LOG CAT AS FOLLOWS

SO guys please help me to solve this problem :D

java.lang.ClassCastException: com.example.surajnath.taproute.UserLocation cannot be cast to com.google.android.gms.location.LocationListener at com.example.surajnath.taproute.UserLocation.startLocationUpdate(UserLocation.java:255) at com.example.surajnath.taproute.UserLocation.onConnected(UserLocation.java:292) at com.google.android.gms.common.internal.zzae.zzk(Unknown Source) at com.google.android.gms.common.api.internal.zzbd.zzj(Unknown Source) at com.google.android.gms.common.api.internal.zzar.zzaht(Unknown Source) at com.google.android.gms.common.api.internal.zzar.onConnected(Unknown Source) at com.google.android.gms.common.api.internal.zzbl.onConnected(Unknown Source) at com.google.android.gms.common.api.internal.zzw.onConnected(Unknown Source) at com.google.android.gm=s.common.intern al.zz+ac.onC++de here`onn+ected(Unknown Source) at com.google.android.gm=s.common.internal.zzn.zzakg(Unknown Source) at com.google.android.gm=s.common.internal.zze.zzv(Unknown Source) at com.google.android.gm=s.common.internal.zzi.zzakh(Unknown Source) at com.google.android.g+ms.common.internal.z+z+h.h+an+d+l+e+M+e+s+s+age(Unknown Source) at android.o+s.Handler.dispatch+Message(Handler.java:102) at android.o+s.L+ooper.loop(Loo+per.java:154) at android.app.Activity+Thread.main(Activity+Thread.java:6682) at java.la+n+g.reflect.Method.invoke(Native Method) at

Now link to my code is below https://pastebin.com/3h3CnHWb


Solution

  • I think you are using the wrong import in UserLocation

    following

    import android.location.LocationListener;
    

    should be

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

    you need this in your gradle dependencies

    implementation 'com.google.android.gms:play-services-location:11.6.0'