Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android locationClient未连接从OnConnected内部引发的异常_Android_Android Geofence_Location Client - Fatal编程技术网

Android locationClient未连接从OnConnected内部引发的异常

Android locationClient未连接从OnConnected内部引发的异常,android,android-geofence,location-client,Android,Android Geofence,Location Client,我有一个使用geo fences的Android应用程序,正在以下代码中添加: public void register(ArrayList<Geofence> geofences){ if(geofences == null || geofences.size() == 0) return; this.geofences = geofences; locationClient = new LocationClient(context,

我有一个使用geo fences的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();
    }
如果出现异常,locationClient未连接,但代码正在从onConnected方法中删除


有人能给我指出正确的方向吗?我的develop Galaxy S4似乎无法重现此错误。

您应该设置连接请求

locationClient.requestLocationUpdates(REQUEST, this);
在这里阅读更多
下面是我如何解决这个问题的。我只是捕获IllegalStateException,然后处理事件,就好像没有位置提供程序一样(ShowDefaultLocationData()是一种在没有GPS可用时处理事件的方法)

locationClient.requestLocationUpdates(REQUEST, this);
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();
    }
}