gpt4 book ai didi

java - 创建 GoogleApiClient 对象时出错,给出 java.lang.IllegalStateException : Already managing a GoogleApiClient with id 0

转载 作者:行者123 更新时间:2023-12-02 08:07:46 25 4
gpt4 key购买 nike

我想在android中实现自动完成位置google API。为此,我需要创建一个 GoogleApiClient 对象。我为此使用了以下代码...

private GoogleApiClient mGoogleApiClient;

mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
.addApi(Drive.API)
.addScope(Drive.SCOPE_FILE)
.enableAutoManage(getActivity(), 0 /* clientId */, this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();

但是当片段启动时,它会抛出以下异常并且应用程序崩溃。

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

注意:这是在 FRAGMENT 中执行的。

以下是 list 文件配置..

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.example.permission.MAPS_RECEIVE"/>

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:name="com.example.bluehorsesoftkol.ekplatevendor.Utils.EkPlateVendorApplication">


<activity
android:name="com.example.bluehorsesoftkol.ekplatevendor.activity.registration.ActivitySplashScreen"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.bluehorsesoftkol.ekplatevendor.activity.registration.ActivityLogin"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".activity.vendor.ActivityHome"
android:screenOrientation="landscape">
</activity>
<activity android:name="com.example.bluehorsesoftkol.ekplatevendor.activity.vendor.ActivityAddVendor"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".activity.vendor.CustomGalleryActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="ekplatevendor.ACTION_PICK" />
<action android:name="ekplatevendor.ACTION_MULTIPLE_PICK" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/TEST_API_KEY"/>

</application>

所以,请帮忙解决这个问题..

谢谢。

最佳答案

在您的代码中尝试一下

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

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

注意:从 Playstore 版本 10 开始,您不必使用

mGoogleApiClient.stopAutoManage(( Activity )上下文);

因为它将由库自行处理。

关于java - 创建 GoogleApiClient 对象时出错,给出 java.lang.IllegalStateException : Already managing a GoogleApiClient with id 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33319479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com