【问题标题】:GoogleApiClient is not connected yet exceptionGoogleApiClient 尚未连接异常
【发布时间】:2015-11-24 10:36:16
【问题描述】:

我正在处理的应用程序,有时可以正常工作,但有时会出现此错误

致命异常: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();
    }

但有时它总是会出现同样的错误,知道为什么会发生这种情况吗?!

【问题讨论】:

  • 你能通过这个吗?

标签: java android google-play-services


【解决方案1】:

这来晚了,但对谁有用:

  1. loc.buildGoogleApiClient(); 移至 onCreate 方法。
  2. loc.createLocationRequest(); 移至 onConnected 方法。

【讨论】:

    【解决方案2】:

    您不应在调用loc.buildGoogleApiClient(); 后立即调用loc.createLocationRequest();,而是在ApiCLient 的onConected() 中调用loc.createLocationRequest()loc.createLocationRequest() 要求在调用 GoogleApiClient 时连接它。

    就此而言,您应该调用任何需要在onConnected() 中连接 APiCLient 的 API。这样可以避免在使用时出现 ApiClient 连接异常。

    【讨论】:

    • 投反对票,很好。我想听听您的答案有什么问题,以便我可以纠正它,甚至人们会为什么......否则它会误导。如果您不能投票。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    相关资源
    最近更新 更多