2015-11-24 24 views
4

我的工作中的应用,工作正常,但有时一些其他时候,它给了我这个错误GoogleApiClient尚未连接异常

致命异常:了java.lang.RuntimeException:无法暂停活动{COM .example.dell.locationapi/com.example.dell.locationapi.MainActivity}:java.lang.IllegalStateException:GoogleApiClient尚未连接。

而有时这种错误:

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

但它一直有时给予同样的错误,任何想法,为什么这发生?!

+0

你能设法通过这个吗? – cgr

回答

0

你不应该调用loc.createLocationRequest();在调用loc.buildGoogleApiClient()后立即生效;相反,您可以在ApiCLient的onConected()中调用loc.createLocationRequest()。 loc.createLocationRequest()需要GoogleApiClient在被调用时连接。

对于这个问题,您应该调用任何需要在onConnected()中连接APiCLient的API。这样,您可以在使用它们时避免ApiClient连接异常。

+0

反对票,不错。我想听听你答案中出了什么问题,所以我可以纠正它,甚至人们会这样做,所以它会误导。如果你不能投票。 – cgr

0

这是迟到但不管是谁,可能是有用的:

  1. 移动loc.buildGoogleApiClient();到onCreate方法。
  2. loc.createLocationRequest();移动到onConnected方法。
相关问题