使用FusedLocationApi时API 23上的应用程序崩溃

时间:2017-06-20 09:56:03

标签: java android google-maps android-fusedlocation

实际上我创建了一个应用程序来显示最近的燃料站,所以我使用FusedLocationApi,并在代码中首先检查使用位置是否启用,如果不是我构建GoogleClient并调用{{1}函数和如果位置是ON比应用程序工作完美,但当位置关闭比应用程序崩溃..请检查日志

日志:

  

E / UncaughtException:java.lang.IllegalStateException:GoogleApiClient   尚未连接。                                                                               在com.google.android.gms.internal.zzbcc.zze(未知来源)                                                                               在com.google.android.gms.internal.zzbcw.zze(未知来源)                                                                               在com.google.android.gms.internal.zzbco.zze(未知来源)                                                                               在   com.google.android.gms.internal.zzcca.requestLocationUpdates(未知   资源)                                                                               在   activities.FuelStations.onConnected(FuelStations.java:356)                                                                               在com.google.android.gms.common.internal.zzac.zzn(未知来源)                                                                               在com.google.android.gms.internal.zzbco.zzm(未知来源)                                                                               在com.google.android.gms.internal.zzbcc.zzpY(未知来源)                                                                               在com.google.android.gms.internal.zzbcc.onConnected(未知来源)                                                                               在com.google.android.gms.internal.zzbcw.onConnected(未知来源)                                                                               在com.google.android.gms.internal.zzbbh.onConnected(未知来源)                                                                               在com.google.android.gms.common.internal.zzaa.onConnected(未知   资源)                                                                               在com.google.android.gms.common.internal.zzn.zzrj(未知来源)                                                                               在com.google.android.gms.common.internal.zze.zzs(未知来源)                                                                               在com.google.android.gms.common.internal.zzi.zzrk(未知来源)                                                                               在com.google.android.gms.common.internal.zzh.handleMessage(未知   资源)                                                                               在android.os.Handler.dispatchMessage(Handler.java:102)                                                                               在android.os.Looper.loop(Looper.java:148)                                                                               在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                               在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

更新的代码:

setingsrequest()

1 个答案:

答案 0 :(得分:0)

您尝试在连接mGoogleApiClient之前访问位置,从连接的方法调用setingsrequest()方法,该方法已在mGoogleApiClient上连接

替换

 @Override
    protected void onResume() {
        super.onResume();

        mGoogleApiClient.connect();

    }

 @Override
    public void onConnected(Bundle bundle) {
     LocationManager mlocManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        final boolean enabled = mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (!enabled) {
            setingsrequest();
        } else {
            init();

        }
    }