Abb*_*bby 5 android exception-handling illegalstateexception
我的应用程序是以地图为中心的.我requestLocationUpdates()
经常打电话.有一段时间,我exception
打电话给它.或者在任何其他称这种方法的地方.我看到SOF上提出的解决方案,遗憾的是似乎没有什么对我有用.即使我打电话mLocationClient.connect()
,也无法保证它会立即连接,如果我错了,请纠正我.我该如何解决这个问题?
案例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;
Run Code Online (Sandbox Code Playgroud)
这是引发的异常:
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)
Run Code Online (Sandbox Code Playgroud)
小智 23
我有一个类似的错误,基本上,上一条评论所说的将解决你的问题.如果您确实跟踪了错误,则会在此处报告:
mLocationClient.requestLocationUpdates(REQUEST, this);
Run Code Online (Sandbox Code Playgroud)
原因是当您调用该API方法时,客户端未连接.这就是为什么之前的答案工作正常,因为他所做的基本上是在回调上启动位置更新.
当你这样做:
mLocationClient.connect();
Run Code Online (Sandbox Code Playgroud)
你的客户端不会立即连接,但代码将继续运行,当你询问locationUpdates时你还没有连接(它仍在使用它,它花费时间),所以它崩溃了.此外,当您执行.connect()时,您基本上会激活onConnected()回调,因此如果您在其中编写locationUpdates请求,那么您基本上将解决大部分错误.
此外,您说您有很多请求,有时只会发生此错误.为此,你没有提供足够的代码,但我想我可以假设错误发生是因为你取消请求并重新激活它们的方式/你的方法(你的方法onStop(),onPause(),onResume()和onStart()).当你暂停你的应用程序时,你应该断开客户端,但你应该确保当你恢复它时,你再次重新连接它,所以也许只是做
mLocationClient.connect();
Run Code Online (Sandbox Code Playgroud)
在onResume()内部,这可能会修复你的一些其他问题(再一次,你没有提供那些代码,也许你已经正确编码了,但对于其他有这个问题的读者,这可能是一个建议,修理它).
这是一个迟到的答案,但希望这有助于社区的其他人.
最好的祝福.
尝试在您的活动上实现GooglePlayServicesClient.ConnectionCallbacks和@Override onConnected(),并将所有需要连接的代码放在其中.
这对我有用.
归档时间: |
|
查看次数: |
11539 次 |
最近记录: |