【问题标题】:Android locationClient not connected exception thrown from inside OnConnected从 OnConnected 内部抛出的 Android locationClient 未连接异常
【发布时间】:2014-07-30 12:36:41
【问题描述】:

我在以下代码中添加了一个使用地理围栏的 Android 应用程序:

public void register(ArrayList<Geofence> geofences){
        if(geofences == null || geofences.size() == 0) return;
        this.geofences = geofences;
        locationClient = new LocationClient(context, this, onConnectionFailedListener);
        locationClient.connect();
    }

    @Override
    public void onConnected(Bundle bundle) {
        locationClient.addGeofences(geofences, GeofenceBroadcastReceiver.getPendingIntent(context), this);
    }

    @Override
    public void onDisconnected() {
        this.locationClient = null;
    }

    @Override
    public void onAddGeofencesResult(int statusCode, String[] strings) {
        if(statusCode != LocationStatusCodes.SUCCESS){
            Log.e(getClass().getName(), "onAddGeofencesResult: " + statusCode);
            return;
        }

        listener.addRegistered(strings);
        locationClient.disconnect();
    }

每隔一段时间我都会收到以下异常(Crashlytics 链接:http://crashes.to/s/b63ac8d5f19):

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
   at com.google.android.gms.internal.ff.bT()
   at com.google.android.gms.internal.hc.addGeofences()
   at com.google.android.gms.location.LocationClient.addGeofences()
   at nl.auxilium.autoreset.GeofenceManager$AddGeofenceManager.onConnected(GeofenceManager.java:140)
   at com.google.android.gms.internal.ff$c.onConnected()
   at com.google.android.gms.internal.fg.b()
   at com.google.android.gms.internal.fg.bV()
   at com.google.android.gms.internal.ff$h.b()
   at com.google.android.gms.internal.ff$h.a()
   at com.google.android.gms.internal.ff$b.eN()
   at com.google.android.gms.internal.ff$a.handleMessage()
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:158)
   at android.app.ActivityThread.main(ActivityThread.java:5751)
   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:1083)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
   at dalvik.system.NativeStart.main(NativeStart.java)

鉴于异常,locationClient 未连接,但代码正在从 onConnected 方法获取。

谁能指出我正确的方向,我似乎无法在我开发的 Galaxy S4 上重现此错误。

【问题讨论】:

    标签: android android-geofence location-client


    【解决方案1】:

    你应该设置连接请求

    locationClient.requestLocationUpdates(REQUEST, this);
    

    在这里阅读更多 https://stackoverflow.com/a/22387816

    【讨论】:

      【解决方案2】:

      以下是我解决此问题的方法。我只是捕获 IllegalStateException,然后处理事件,就好像没有位置提供程序存在(ShowDefaultLocationData() 是在没有 GPS 可用时处理事情的方法)。

      private void requestLocation() {
              if (servicesConnected()) {
                  LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
                  if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                      updateLocation = true;
                      mLocationRequest = LocationRequest.create();
                      mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                      mLocationRequest.setInterval(5000);
                      mLocationRequest.setFastestInterval(1000);
                      mLocationClient = new LocationClient(this, this, this);
                      mLocationClient.connect();
                  }
              } else
              {
                  ShowDefaultLocationData();
              }
          }
      
      
      @Override
      public void onConnected(Bundle connectionHint) {
          try {
              mLocationClient.requestLocationUpdates(mLocationRequest, MainActivity.this);
              isconnected = true;
          } catch (IllegalStateException e) {
              ShowDefaultLocationData();
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-26
        • 1970-01-01
        • 1970-01-01
        • 2018-08-31
        • 1970-01-01
        • 2015-06-03
        相关资源
        最近更新 更多