简体   繁体   English

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

[英]Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet

We have this crash in crashlytics, the weird thing is it happens in onConnected() callback when requesting location updates. 我们在crashlytics中遇到了这种崩溃,奇怪的是它在请求位置更新时发生在onConnected()回调中。

Code: 码:

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

Crash log: 崩溃日志:

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)

Doesn't onConnected() imply GoogleApiClient is connected and ready to be used? onConnected()是否意味着GoogleApiClient已连接并准备好使用? How can I resolve this? 我该如何解决这个问题?

https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html 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. 您应该在Activity的onCreate(Bundle)方法中实例化一个客户端对象,然后在onStart()中调用connect(),在onStop()中调用disconnect(),而不管状态如何。

The implementation of the GoogleApiClient appears designed for only a single instance. GoogleApiClient的实现似乎仅针对单个实例设计。 It's best to instantiate it only once in onCreate, then perform connections and disconnections using the single instance. 最好在onCreate中仅实例化一次,然后使用单个实例执行连接和断开连接。

Maybe you should notice, GoogleApiClient.Builder has a method setAccountName() . 也许您应该注意, GoogleApiClient.Builder有一个方法setAccountName() You should invoke this method with your Google account name. 您应该使用您的Google帐户名称调用此方法。 I have tried, and succeeded. 我已经尝试过,并且成功了。 I used the following code: 我使用了以下代码:

if (mGoogleApiClient == null) {
    mGoogleApiClient = new GoogleApiClient.Builder(context)
       .addApi(LocationServices.API)
       .addConnectionCallbacks(this)
       .addOnConnectionFailedListener(this)
       .setAccountName("李江涛")
       .build();
}

if (mLocationRequest == null) {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

Try using com.google.android.gms.location.LocationClient instead of GoogleApiClient . 尝试使用com.google.android.gms.location.LocationClient而不是GoogleApiClient Note that the ConnectionCallbacks and OnConnectionFailedListener interfaces that you implement will be slightly different. 请注意,您实现的ConnectionCallbacksOnConnectionFailedListener接口将略有不同。

Here's a quick example: 这是一个简单的例子:

class LocationHandler implements ConnectionCallbacks,
        OnConnectionFailedListener, LocationListener {

    private LocationClient client;

    void getLocation(Context context) {
        client = new LocationClient(context, this, this);
        client.connect();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        LocationRequest request = LocationRequest.create();
        request.setNumUpdates(1);
        client.requestLocationUpdates(request, this);
        client.unregisterConnectionCallbacks(this);
    }

    @Override
    public void onDisconnected() { }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        // handle connection failure
    }

    @Override
    public void onLocationChanged(Location location) {
        client.removeLocationUpdates(this);
        client.disconnect();

        // do stuff with the location
    }

}

There's an issue with your code here: 您的代码存在问题:

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

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

Simply clean it up a bit: 简单地清理一下:

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

if (googleApiClient != null) {
  if (googleApiClient.isConnected()) {
    maintain boolean;
new intent(pass.through.filter(widgetBank.css(openbridge.jar))
<access bridge.java>cascadeRunTime(true)
    googleApiClient.disconnect();
if situation(positive) {
//Call on filter
return false
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:尚未连接GoogleApiClient - java.lang.IllegalStateException: GoogleApiClient is not connected yet java.lang.IllegalStateException:GoogleApiClient尚未连接 - java.lang.IllegalStateException: GoogleApiClient is not connected yet 引起:java.lang.IllegalStateException:GoogleApiClient 尚未连接 - Caused by: java.lang.IllegalStateException: GoogleApiClient is not connected yet java.lang.IllegalStateException:必须连接GoogleApiClient - java.lang.IllegalStateException: GoogleApiClient must be connected 异常:java.lang.IllegalStateException:已连接 - Exception : java.lang.IllegalStateException: Already connected 致命异常(java.lang.IllegalStateException) - Fatal exception (java.lang.IllegalStateException) 致命异常:java.lang.IllegalStateException没有活动 - Fatal Exception: java.lang.IllegalStateException No activity Android:Google Play游戏服务连接错误(java.lang.IllegalStateException:必须连接GoogleApiClient。) - Android: Google play games services connection error ( java.lang.IllegalStateException: GoogleApiClient must be connected.) 线程“main”中的异常 java.lang.IllegalStateException:已连接 - Exception in thread “main” java.lang.IllegalStateException: Already connected 致命异常:主java.lang.IllegalStateException当按Back时 - FATAL EXCEPTION: main java.lang.IllegalStateException When press Back
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM