2014-01-14 4 views
5

내 앱은지도 중심적이다. 자주 requestLocationUpdates() 번으로 전화합니다. 가끔씩, 전화가 걸릴 때 exception이 나옵니다. 또는 그러한 방법을 호출하는 다른 장소에서. SOF에서 제안 된 솔루션을 보았습니다. 슬프게도 나를 위해 아무 것도 작동하지 않는 것 같습니다. 내가 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() 및 onResume onStart()). 앱을 일시 중지하면, 클라이언트의 연결을 해제해야합니다,하지만 당신은 당신이 그것을 다시 시작하면, 다시 그것을 다시 연결 있는지 확인해야합니다, 그래서 아마 바로 onResume (내부

mLocationClient.connect(); 

을)하고 있음을 가능성이 일부 수정 다른 문제 (다시 한번, 당신은 그 코드를 제공하지 않았다. 아마도 당신은 이미 그것을 코딩하고 있을지도 모른다. 그러나이 문제를 가지고있는 다른 독자들에게는 이것을 고칠 수있는 제안 일 것이다).

다소 늦은 대답이지만, 나머지 커뮤니티에 도움이되기를 바랍니다.

감사합니다.

+1

위대한 설명, 감사합니다! – Abby

+1

이러한 종류의 설명은 항상 높이 평가됩니다. 감사! – 0x5f3759df

+0

onConnected() 메서드를 사용하는 경우에도 같은 줄의 코드에서이 오류가 발생했습니다. 그게 뭐야? – Price

9

시도가에 GooglePlayServicesClient.ConnectionCallbacks 및 @Override onConnected()를 구현하기 위해 액티비티를 만들고 그 안에 연결이 필요한 모든 코드를 넣습니다.

저에게 도움이되었습니다.