java.lang.IllegalStateException:GoogleApiClient尚未连接

时间:2017-04-07 09:24:39

标签: java google-api google-api-java-client fusedlocationproviderapi

错误消息

FATAL EXCEPTION: main
    java.lang.IllegalStateException: GoogleApiClient is not connected yet.
       at com.google.android.gms.internal.zzlg.zzb(Unknown Source)
       at com.google.android.gms.internal.zzli.zzb(Unknown Source)
       at com.google.android.gms.location.internal.zzd.requestLocationUpdates(Unknown Source)
       at com.example.argede06.argede.KonumServis_KonumCek.Konum_GoogleApi.startLocationUpdate(Konum_GoogleApi.java:82)
       at com.example.argede06.argede.KonumServis_KonumCek.Konum_GoogleApi$1.run(Konum_GoogleApi.java:62)
       at android.os.Handler.handleCallback(Handler.java:725)
       at android.os.Handler.dispatchMessage(Handler.java:92)
       at android.os.Looper.loop(Looper.java:153)
       at android.app.ActivityThread.main(ActivityThread.java:5336)
       at java.lang.reflect.Method.invokeNative(Native Method)
       at java.lang.reflect.Method.invoke(Method.java:511)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
       at dalvik.system.NativeStart.main(Native Method)

代码

public class Konum_GoogleApi extends Service implements ConnectionCallbacks,OnConnectionFailedListener,LocationListener
{

    int mStartMode;
    protected GoogleApiClient mgoogleApiClient;
    protected LocationRequest mLocationRequest;
    protected Location location;
    private Handler mServiceHandler;
    private static final String TAG = Konum_GoogleApi.class.getSimpleName();
    @Override
    public void onCreate() {
        Log.e("BuildAPi googlclient","sycbhorid");
        mgoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mgoogleApiClient.connect();
         createRequest();
        Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() {
            @Override
            public void run() {
        startLocationUpdate();
            }
        });

        HandlerThread handlerThread = new HandlerThread(TAG);
        handlerThread.start();
        mServiceHandler = new Handler(handlerThread.getLooper());

    }

    protected void  createRequest(){
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(0);
        mLocationRequest.setFastestInterval(0);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }
    protected void startLocationUpdate(){
        Log.i(TAG, "Requesting location updates");

        try {
            LocationServices.FusedLocationApi.requestLocationUpdates(mgoogleApiClient, mLocationRequest,(com.google.android.gms.location.LocationListener)this);
        } catch (SecurityException unlikely) {
            Log.e(TAG, "Lost location permission. Could not request updates. " + unlikely);
        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onConnected(Bundle bundle) {
        Log.e("Connect","onconnected");
        if(location == null){
            location = LocationServices.FusedLocationApi.getLastLocation(mgoogleApiClient);
        }
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.e(TAG, "GoogleApiClient connection suspended.");
    }

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

    @Override
    public void onLocationChanged(Location location) {
        Log.e("(Google)Kordinatlar =",String.valueOf(location.getLongitude())+","+String.valueOf(location.getLatitude()));

    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }
    @Override
    public void onDestroy(){
        mServiceHandler.removeCallbacksAndMessages(null);
        mgoogleApiClient.disconnect();
    }
}

StartLocationUpdate中的GoogleApiClient错误保护无效

    ///this eror   LocationServices.FusedLocationApi.requestLocationUpdates(mgoogleApiClient, mLocationRequest,(com.google.android.gms.location.LocationListener)this);

我无法在服务上运行activiy。我无法在服务上运行位置更新。我经常遇到这个错误。如何解决此错误?

1 个答案:

答案 0 :(得分:0)

在客户端连接之前,您正在呼叫createRequestmgoogleApiClient.connect();是异步的,不会立即连接到客户端,将createRequest();移动到onConnected方法,以便在连接API客户端时发送位置请求。