创建GoogleApiClient对象时出错,给出java.lang.IllegalStateException:已经管理了ID为0的GoogleApiClient

时间:2015-10-24 14:47:24

标签: java google-api illegalstateexception

我想在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:已经在管理GoogleApiClient   id为

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

以下是清单文件配置..

<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> 

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

谢谢。

2 个答案:

答案 0 :(得分:12)

在您的代码中尝试此操作

@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) context);

因为它将由图书馆自己照顾。

答案 1 :(得分:0)

这就是我为片段制作的方式:

片段代码:

private synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .enableAutoManage(getActivity(), this)
            .addConnectionCallbacks(this)
            .addApi(LocationServices.API)
            .build();
    createLocaitonRequest();
}

private void createLocaitonRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

onStart for the fragment:

@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
片段的

onDestroy:

@Override
public void onDestroy() {
    super.onDestroy();
    mGoogleApiClient.stopAutoManage(getActivity());
    mGoogleApiClient.disconnect();
}

我在 onCreate()

中运行 buildGoogleApiClient()方法

然后我在我的XML中使用MapView作为我的片段,如下所示:

<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />

然后在 onCreateView 的片段中,我执行以下操作:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.fragment_maps, container, false);
    setRetainInstance(true);
    setupMapsFragment(mView);

    mMapView = (MapView) mView.findViewById(R.id.map);
    if (mMapView != null) {
        mMapView.onCreate(null);
        mMapView.onResume();
        mMapView.getMapAsync(this);
    }

    return mView;
}

如果使用导航工具进行导航,我在加载片段时遇到了问题,然后再次按下导航菜单项,然后崩溃,以解决我这样做:

if (id == R.id.nav_map) {

        if(checkMapsFragment == true){
            Toast.makeText(this,"map is already loaded",Toast.LENGTH_LONG).show();
        }
        if(checkMapsFragment == false){
            fragment = new MapsFragment();
            checkMapsFragment = true;
        }