2

錯誤信息java.lang.IllegalStateException:GoogleApiClient尚未連接

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) 

CODE

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

GoogleApiClient錯誤StartLocationUpdate線保護無效

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

我無法運行activiy在服務。我無法在服務上運行位置更新。我不斷得到這個錯誤。我該如何解決這個錯誤?

+0

在帖子末尾加強了提問。還添加了相關標籤。 –

回答

0

您在客戶端連接之前撥打createRequestmgoogleApiClient.connect();是異步的,並且不會立即連接到客戶端,請將createRequest();移動到onConnected方法,即API客戶端連接時將發送位置請求。

相關問題