GoogleApiClient未连接但异常 [英] GoogleApiClient is not connected yet exception
问题描述
我正在使用的应用程序,有时可以正常工作,但有些时候它会给我这个错误。
$ b blockquote>
致命异常:java。 lang.RuntimeException:无法暂停活动{com.example.dell.locationapi / com.example.dell.locationapi.MainActivity}:java.lang.IllegalStateException:GoogleApiClient尚未连接。
blockquote>
有时会出现这样的错误:
java.lang.IllegalStateException:GoogleApiClient未连接即使调用了
onConnected()
函数,
也是如此。
我将 buildGoogleApiClient()
调用移动到 onCreate()
的开头,这里是我的代码:
protected synchronized void buildGoogleApiClient(){
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks (this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
调用 onCreate()
:
loc = new MyLocation(MainActivity.this);
if(loc.checkPlayServices()){
loc.buildGoogleApiClient();
loc.createLocationRequest();
}
但它会一直给出相同的错误,为什么会发生这种情况?
loc.buildGoogleApiClient();
移至onCreate方法。 $ c> loc.createLocationRequest(); 添加到onConnected方法。
The application i'm working on, works properly sometimes but some other times it gives me this error
FATAL EXCEPTION: java.lang.RuntimeException: Unable to pause activity {com.example.dell.locationapi/com.example.dell.locationapi.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
And sometimes this error :
java.lang.IllegalStateException: GoogleApiClient is not connected yet.
even though the onConnected()
function is called.
I moved the calling of buildGoogleApiClient()
to the begining ofonCreate()
here is my code:
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API).build();
}
calling the methods in onCreate()
:
loc = new MyLocation(MainActivity.this);
if (loc.checkPlayServices()) {
loc.buildGoogleApiClient();
loc.createLocationRequest();
}
but it keeps giving the same error sometimes, any idea why this happens?!
This is coming late but for whoever it might be useful for:
- Move
loc.buildGoogleApiClient();
to the onCreate method. - Move
loc.createLocationRequest();
to the onConnected method.
这篇关于GoogleApiClient未连接但异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!