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

后端 未结 4 655
轮回少年
轮回少年 2020-12-10 11:16

We have this crash in crashlytics, the weird thing is it happens in onConnected() callback when requesting location updates.

Code:

相关标签:
4条回答
  • 2020-12-10 11:34

    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
      }
    
    0 讨论(0)
  • 2020-12-10 11:40

    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.

    The implementation of the GoogleApiClient appears designed for only a single instance. It's best to instantiate it only once in onCreate, then perform connections and disconnections using the single instance.

    0 讨论(0)
  • 2020-12-10 11:46

    Maybe you should notice, GoogleApiClient.Builder has a method setAccountName(). You should invoke this method with your Google account name. 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);
    }
    
    0 讨论(0)
  • 2020-12-10 11:48

    Try using com.google.android.gms.location.LocationClient instead of GoogleApiClient. Note that the ConnectionCallbacks and OnConnectionFailedListener interfaces that you implement will be slightly different.

    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
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题