java - 致命异常 : java. lang.IllegalStateException GoogleApiClient 尚未连接

标签 java android google-play-services

我们在 crashlytics 中遇到了这个崩溃,奇怪的是它发生在请求位置更新时的 onConnected() 回调中。

代码:

abstract public class MainService_6_LocationClient extends MainService_5_DriverGpsLocationStoring
    implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {


  private LocationListener highAccuracyListener;
  private GoogleApiClient googleApiClient;
  private LocationRequest gpsRequest;

  @Override
  public void onCreate() {
    super.onCreate();
    lazyInit();
    googleApiClient.connect();
  }

  @Override public void onConnected(Bundle bundle) {
    Log.d(TAG, "onConnected");
    lazyInit();
    LocationServices.FusedLocationApi.requestLocationUpdates(googleApiClient, gpsRequest,
          highAccuracyListener);
  }

  private void lazyInit() {

    if (highAccuracyListener == null) {
      highAccuracyListener = new HighAccuracyLocationListener();
    }

    if (googleApiClient == null) {
      googleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API)
          .addConnectionCallbacks(this)
          .addOnConnectionFailedListener(this)
          .build();
    }

    if (gpsRequest == null) {
      gpsRequest = new LocationRequest().setInterval(2000)
          .setFastestInterval(1000)
          .setSmallestDisplacement(0)
          .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }
  }

  @Override public void onConnectionSuspended(int i) {
    Log.w(TAG, "onConnectionSuspended");
    lazyInit();
    googleApiClient.reconnect();
  }

  @Override public void onConnectionFailed(ConnectionResult connectionResult) {
    Log.e(TAG, "onConnectionFailed");
  }

  @Override public void onDestroy() {
    Log.d(TAG, "onDestroy");

    if (googleApiClient != null) {
      if (googleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(googleApiClient,
            highAccuracyListener);
        googleApiClient.disconnect();
      }

      googleApiClient = null;
    }
    highAccuracyListener = null;
    gpsRequest = null;

    super.onDestroy();
  }

崩溃日志:

java.lang.IllegalStateException: GoogleApiClient is not connected yet.
       at com.google.android.gms.common.internal.o.a()
       at com.google.android.gms.common.api.b.b()
       at com.google.android.gms.internal.lu.requestLocationUpdates()
       at ee.mtakso.driver.service.orderState.MainService_6_LocationClient.onConnected(MainService_6_LocationClient.java:33)
       at com.google.android.gms.common.internal.f.d()
       at com.google.android.gms.common.api.b.gm()
       at com.google.android.gms.common.api.b.d()
       at com.google.android.gms.common.api.b$2.onConnected()
       at com.google.android.gms.common.internal.f.d()
       at com.google.android.gms.common.internal.f.dL()
       at com.google.android.gms.common.internal.e$h.b()
       at com.google.android.gms.common.internal.e$h.g()
       at com.google.android.gms.common.internal.e$b.gU()
       at com.google.android.gms.common.internal.e$a.handleMessage()
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loop(Looper.java:137)
       at android.app.ActivityThread.main(ActivityThread.java:4947)
       at java.lang.reflect.Method.invokeNative(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
       at dalvik.system.NativeStart.main(NativeStart.java)

onConnected() 是否暗示 GoogleApiClient 已连接并可以使用?我该如何解决这个问题?

最佳答案

https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html

You should instantiate a client object in your Activity's onCreate(Bundle) method and then call connect() in onStart() and disconnect() in onStop(), regardless of the state.

GoogleApiClient 的实现似乎仅针对单个实例而设计。最好在 onCreate 中只实例化一次,然后使用单个实例执行连接和断开连接。

关于java - 致命异常 : java. lang.IllegalStateException GoogleApiClient 尚未连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26998201/

相关文章:

java - Spring-AOP 切入点不起作用?

java - 将大型 gzip 数据文件上传到 HDFS

android - 程序类型已经存在 : org. apache.commons.logging.Log

android - 使用 ORMLite 填充 ListView

android - 权限名称 C2D_MESSAGE 不唯一出现在两个 C2D_MESSAGE 中

java - 将新的 Google 登录添加到 Android 应用程序后的 Proguard 混淆问题

android - 解决 AdMobs 问题

java - 在 Robot Framework 中进行断言的最佳方法是什么?

java - 是否可以在连接到 Android Studio 并从 Android Studio 运行的 Android 应用程序中测试应用内计费

Java多态练习