Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建GoogleAppClient对象时出错,给出java.lang.IllegalStateException:已在管理id为0的GoogleAppClient_Java_Google Api_Illegalstateexception - Fatal编程技术网

创建GoogleAppClient对象时出错,给出java.lang.IllegalStateException:已在管理id为0的GoogleAppClient

创建GoogleAppClient对象时出错,给出java.lang.IllegalStateException:已在管理id为0的GoogleAppClient,java,google-api,illegalstateexception,Java,Google Api,Illegalstateexception,我想在android中实现自动完成google API。为此,我需要创建一个GoogleAppClient对象。我已经为此使用了以下代码 private GoogleApiClient mGoogleApiClient; mGoogleApiClient = new GoogleApiClient.Builder(getActivity()) .addApi(Drive.API) .addScope(Drive.SCOPE_FIL

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

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:已在管理GoogleAppClient id为0的

注意:这是在片段中执行的。

以下是清单文件配置

<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开始,您不必使用

mgoogleapclient.stopAutoManage((活动)上下文)


因为它将由图书馆自己保管。

这是我如何为我的片段制作的:

片段的代码:

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);
}
@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
@Override
public void onDestroy() {
    super.onDestroy();
    mGoogleApiClient.stopAutoManage(getActivity());
    mGoogleApiClient.disconnect();
}
片段的onStart:

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);
}
@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
@Override
public void onDestroy() {
    super.onDestroy();
    mGoogleApiClient.stopAutoManage(getActivity());
    mGoogleApiClient.disconnect();
}
片段的onDestroy:

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);
}
@Override
public void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}
@Override
public void onDestroy() {
    super.onDestroy();
    mGoogleApiClient.stopAutoManage(getActivity());
    mGoogleApiClient.disconnect();
}
我在onCreate()中运行buildGoogleAppClient()方法

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

<com.google.android.gms.maps.MapView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/map" />
如果导航是使用navigationdrawer进行的,那么当我加载片段,然后再次按下导航菜单项,然后它崩溃时,我确实遇到了您的问题,为了解决这个问题,我执行了以下操作:

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

还是有同样的错误,你能检查一下@Ravi吗