Search code examples
androidlocationgoogle-api-clientfusedlocationproviderapiandroid-fusedlocation

android clashed between `LocationServices.API` and `Auth.GOOGLE_SIGN_IN_API` when using `FusedLocationApi`


I have code setGoogleSignInAccount like this

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

and i have code for get location using FusedLocationApilike this

Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                            mGoogleApiClient);

and it's work but my question is now when i click logout error because .addApi(LocationServices.API) when i changes to .addApi(Auth.GOOGLE_SIGN_IN_API, gso) the logout is success but now the FusedLocationApi is error

and this logcat when FusedLocationApi error

07-17 11:58:16.763 26607-26607/com.emergency.e_place E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.emergency.e_place, PID: 26607
                                                                       java.lang.NullPointerException: Appropriate Api was not requested.
                                                                           at com.google.android.gms.common.internal.zzx.zzb(Unknown Source)
                                                                           at com.google.android.gms.common.api.internal.zzj.zza(Unknown Source)
                                                                           at com.google.android.gms.location.LocationServices.zzi(Unknown Source)
                                                                           at com.google.android.gms.location.internal.zzd.getLastLocation(Unknown Source)
                                                                           at com.emergency.e_place.MainActivity$1.onClick(MainActivity.java:166)
                                                                           at android.view.View.performClick(View.java:4646)
                                                                           at android.view.View$PerformClick.run(View.java:19403)
                                                                           at android.os.Handler.handleCallback(Handler.java:733)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:146)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5511)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                                                                           at dalvik.system.NativeStart.main(Native Method)

Solution

  • when i changes to .addApi(Auth.GOOGLE_SIGN_IN_API, gso) the logout is success

    Don't change. Add.

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .addApi(LocationServices.API)
                    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
                    // Add more APIs, as needed
                    .build();