Google Api客户端有时在onConnected中为NULL

qua*_*n91 18 android google-api-client

我实施GoogleApiClient如下:

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

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

  java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0
Run Code Online (Sandbox Code Playgroud)

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

谢谢!

更新

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

谢谢大家的帮助.

Ben*_*old 36

我有同样的问题.所有我解决它的方法都是删除,.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();
}
Run Code Online (Sandbox Code Playgroud)

从技术上讲,这.enableAutoManage(this, 0 /* clientId */, this)应该是应该做的,除了现在,一切都按预期工作.


abe*_*far 13

文档说:在任何给定时间,每个id只允许一个自动管理的客户端.要重复使用ID,您必须先调用stopAutoManage(FragmentActivity)以前的客户端.

我个人所做的就是在离开活动之前打电话给bellow方法,我正在使用Google Api Client.

private void stopAutoManage() {
    if (mGoogleApiClient != null)
        mGoogleApiClient.stopAutoManage(mActivity);
}
Run Code Online (Sandbox Code Playgroud)


小智 6

我想你最好看这个参考.

"public GoogleApiClient.Builder enableAutoManage"的参考页面

在此页面中显示,如果clientId已经被自动管理,则通过IllegalStateException.所以,检查你的代码

                .enableAutoManage(this, 0 /* clientId */, this)
Run Code Online (Sandbox Code Playgroud)

我认为如果你的代码有异常,它可能会因为没有完成而返回零.