2014-01-14 21 views
5

我的應用程序是以地圖爲中心的。我經常撥打requestLocationUpdates()。有一段時間,當它被調用時,我得到這個exception。或者在任何其他調用這種方法的地方。我看到了特種部隊提出的解決方案,遺憾的是似乎沒有任何東西可以用於我。即使我打電話給mLocationClient.connect(),也不能保證它立即連接,糾正我,如果我錯了。我如何解決這個問題?未連接。調用Connect或等待onConnected()被調用

情況下R.id.btnContinue:

gpsLocationDailog.cancel(); 

     int isGooglePlayServiceAvilable = GooglePlayServicesUtil 
       .isGooglePlayServicesAvailable(getApplicationContext()); 
     if (isGooglePlayServiceAvilable == ConnectionResult.SUCCESS) { 
      /** thorws shitty expectiosn **/ 
      mLocationClient.connect(); 
      try { 
       mLocationClient.requestLocationUpdates(REQUEST, this); 
       mCurrentLocation = mLocationClient.getLastLocation(); 
       fireQueryToGetTheResponse(latitude, longitude); 
       rl.setVisibility(View.VISIBLE); 
       mDrawerLayout.setVisibility(View.GONE); 
       mSlidingUpPanelLayout.setVisibility(View.GONE); 
      } catch (IllegalStateException e) { 
       mLocationClient.connect(); 
       Log.e(TAG, " Waiting for onConnect to be called"); 
      } 
     } else { 
      GooglePlayServicesUtil.getErrorDialog(
        isGooglePlayServiceAvilable, 
        SqueakeeMapListViewPager.this, 0).show(); 
     } 

     break; 

,這是引發的異常:

java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called. 
at com.google.android.gms.internal.de.bc(Unknown Source) 
at com.google.android.gms.internal.ez.a(Unknown Source) 
at com.google.android.gms.internal.ez$c.bc(Unknown Source) 
at com.google.android.gms.internal.ey.requestLocationUpdates(Unknown Source) 
at com.google.android.gms.internal.ez.requestLocationUpdates(Unknown Source) 
at com.google.android.gms.internal.ez.requestLocationUpdates(Unknown Source) 
at com.google.android.gms.location.LocationClient.requestLocationUpdates(Unknown Source) 
at org.application.app.squeakee.SqueakeeMapListViewPager.onClick(SqueakeeMapListViewPager.java:1936) 
at android.view.View.performClick(View.java:4475) 
at android.view.View$PerformClick.run(View.java:18786) 
at android.os.Handler.handleCallback(Handler.java:730) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
at android.os.Looper.loop(Looper.java:137) 
at android.app.ActivityThread.main(ActivityThread.java:5493) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:525) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) 
at dalvik.system.NativeStart.main(Native Method) 

回答

23

我有一個類似的錯誤,基本上,以前的評論說什麼會解決你的問題是的。 如果你沒有跟蹤你的錯誤,那就到這裏報到:

mLocationClient.requestLocationUpdates(REQUEST, this); 

的原因是,當你調用API方法的客戶端未連接。這就是爲什麼以前的答案工作得很好,因爲基本上他所做的就是在回調中啓動位置更新。

當你這樣做:

mLocationClient.connect(); 

您的客戶端將不會立即連接,但該代碼將保持正在運行,而當你問locationUpdates您還沒有連接(仍能工作就可以了,它需要時間),所以它崩潰了。另外,當你執行.connect()時,你基本上會激活onConnected()回調函數,所以如果你在它內部編寫你的locationUpdates請求,那麼你基本上可以解決你的大部分錯誤。

此外,你說你有很多請求,只有有時會出現這個錯誤。爲此,您沒有提供足夠的代碼,但是我認爲我可以認爲由於取消請求並重新激活它們(您的方法onStop(),onPause(),onResume()和onStart())。當您暫停您的應用程序,你應該斷開客戶端,但你應該確保,當你恢復它,你再重新連接,所以也許只需要做

mLocationClient.connect(); 

中的onResume(內),而且可能修復了一些你的其他問題(再一次,你沒有提供該代碼,也許你已經編寫了正確的代碼,但對於其他有這個問題的讀者,這可能是一個解決它的建議)。

這有點遲來的答案,但希望這有助於其他社區。

此致敬禮。

+1

很好的解釋,謝謝!! – Abby

+1

這些種類的解釋總是值得讚賞的。謝謝! – 0x5f3759df

+0

即使它在onConnected()方法中,我也在同一行代碼中得到了此錯誤。你怎麼做的? – Price

9

嘗試在實施GooglePlayServicesClient.ConnectionCallbacks和@覆蓋onConnected()你活動並將所有需要連接的代碼放在裏面。

這對我有用。