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

问题描述 投票:18回答:6

我实现qazxsw poi如下:

GoogleApiClient

但在 mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this, 0 /* clientId */, this) .addApi(LocationServices.API) .addApi(Places.GEO_DATA_API) .addConnectionCallbacks(this) .build(); 方法中,我检查mGoogleApiClient => value null。在这种情况下,我尝试重新构建googleApiClient但我收到错误:

onConnected

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

谢谢!

更新

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

谢谢大家的帮助。

android google-api-client
6个回答
36
投票

我有同样的问题。我所做的就是解决它是删除 java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0 ,因为它根本无法正常工作。然后,在您的活动中覆盖这些方法:

.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(); } 应该做的事情,除了现在,一切都按预期运作。


13
投票

.enableAutoManage(this, 0 /* clientId */, this)说:在任何给定的时间,每个id只允许一个自动管理的客户端。要重复使用ID,您必须先在以前的客户端上调用Documentation

在我离开活动之前,我个人所做的就是打电话给波纹管方法,我正在使用谷歌Api客户端。

stopAutoManage(FragmentActivity)

6
投票

我想你最好看这个参考。

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

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

reference page of "public GoogleApiClient.Builder enableAutoManage"

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


2
投票

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

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

使用

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

它会工作正常


1
投票

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

  • 通常覆盖mGoogleApiClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addApi(Places.GEO_DATA_API) .addConnectionCallbacks(this) .build(); onStart()
  • 加入onStop(),请致电onStop()

祝你今天愉快...


0
投票

如果你已经连接,yourApiGoogle.stopAutoManage(context);立即打电话给build()。因此,您的变量可能为null。

更好用

onConnected
© www.soinside.com 2019 - 2024. All rights reserved.