当使用`FusedLocationApi`时,android在`LocationServices.API`和`Auth.GOOGLE_SIGN_IN_API`之间发生冲突

时间:2016-07-17 05:00:28

标签: android location google-api-client fusedlocationproviderapi android-fusedlocation

我有像这样的代码setGoogleSignInAccount

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

我有使用FusedLocationApi这样的

来获取位置的代码
Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                            mGoogleApiClient);

并且它的工作正常,但我现在的问题是,当我点击退出错误时.addApi(LocationServices.API)当我更改为.addApi(Auth.GOOGLE_SIGN_IN_API, gso)时,退出成功,但现在FusedLocationApi是错误

FusedLocationApi错误

时的此logcat
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)

1 个答案:

答案 0 :(得分:1)

  

当我更改为.addApi(Auth.GOOGLE_SIGN_IN_API,gso)时,注销成功

不要改变。加。

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();