【问题标题】:Google Api Client sometime NULL in onConnectedGoogle Api 客户端有时在 onConnected 中为 NULL
【发布时间】:2015-04-14 04:30:36
【问题描述】:

我如下实现GoogleApiClient

 mGoogleApiClient = new GoogleApiClient.Builder(this)
                .enableAutoManage(this, 0 /* clientId */, this)
                .addApi(LocationServices.API)
                .addApi(Places.GEO_DATA_API)
                .addConnectionCallbacks(this)
                .build();

但在onConnected 方法中,我检查了 mGoogleApiClient => value null。 在这种情况下,我尝试重新构建 googleApiClient 但我收到错误:

  java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0

请帮助我理解为什么 mGoogleApiClient 尽管它已连接但有时为 NULL:|。 (注意。我检查了所有源代码,我从未将 GoogleApiClient 设置为 NULL)。

谢谢!

更新

在我尝试使用最新版本的播放服务后,我的问题现在解决了。

感谢大家的帮助。

【问题讨论】:

  • 我有同样的问题,但我使用的是 google play servcies,8.4.0 最新版本,但这个问题仍然存在

标签: android google-api-client


【解决方案1】:

我遇到了同样的问题。我为解决它所做的只是删除.enableAutoManage(this, 0 /* clientId */, this),因为它无法正常工作。然后,在您的活动中覆盖这些方法:

@Override
public void onStart() {
    super.onStart();
    if (mGoogleApiClient != null) {
        mGoogleApiClient.connect();
    }
}

@Override
public void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

从技术上讲,这就是 .enableAutoManage(this, 0 /* clientId */, this) 应该做的,除了现在,一切正常。

【讨论】:

    【解决方案2】:

    Documentation 说:在任何给定时间,每个 id 只允许一个自动管理的客户端。要重用 id,您必须首先在前一个客户端上调用 stopAutoManage(FragmentActivity)

    我个人所做的是在我离开活动之前调用 bellow 方法,我正在使用 Google Api 客户端。

    private void stopAutoManage() {
        if (mGoogleApiClient != null)
            mGoogleApiClient.stopAutoManage(mActivity);
    }
    

    【讨论】:

    • 仍然出现同样的错误,你能检查this question请@abedfar
    【解决方案3】:

    我认为你最好看看这个参考。

    reference page of "public GoogleApiClient.Builder enableAutoManage"

    在此页面中显示,如果 clientId 已经被自动管理,则通过 IllegalStateException。 所以,用

    检查你的代码
                    .enableAutoManage(this, 0 /* clientId */, this)
    

    我认为如果您的代码出现异常,它可能会返回零作为未完成。

    【讨论】:

    • 只有在我重建 Google API 时才会发生。但是目前我将其构建完成。方法“onConnected”调用。但是 mGooogleApiClient 是 NULL :|我调试并检查了很多次。 (并不总是发生。Feq 约为 1/10)。
    • 我明白为什么会发生错误“java.lang.IllegalStateException:已经在管理 ID 为 0 的 GoogleApiClient”。我关心为什么 mGoogleApiClient 在 onConnected 方法中具有 NULL 值:|,请帮助。谢谢
    【解决方案4】:

    如果您在尝试重新初始化 mGoogleApiClient 时遇到此问题,则只需删除

    .enableAutoManage(this, 0 /* clientId */, this)

    使用

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LocationServices.API)
                .addApi(Places.GEO_DATA_API)
                .addConnectionCallbacks(this)
                .build();
    

    它会正常工作

    【讨论】:

    • 删除 enableAutoManage 后没有任何反应,但添加后可以正常工作
    【解决方案5】:

    我在一个片段中遇到了同样的问题(已经在管理一个 id 为 0 的 GoogleApiClient),最后我解决了:

    • 正常覆盖onStart()onStop()
    • 加入onStop() 拨打yourApiGoogle.stopAutoManage(context);

    祝你有美好的一天......

    【讨论】:

      【解决方案6】:

      如果您已经连接,build() 会立即呼叫onConnected。因此,您的变量可能为空。

      更好的使用

      mGoogleApiClient = new GoogleApiClient.Builder(this)
                  .enableAutoManage(this, 0 /* clientId */, this)
                  .addApi(LocationServices.API)
                  .addApi(Places.GEO_DATA_API)
                  .addConnectionCallbacks(this);
      mGoogleApiClient.build();
      

      【讨论】:

        猜你喜欢
        • 2016-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多