在 Fragment 中使用 GoogleApiClient 时递归进入 executePendingTransactions

Recursive entry to executePendingTransactions on using GoogleApiClient in Fragment

我正在尝试在我的应用程序中使用地点自动完成 API。我有一个主要 Activity。我有一个 Fragment,它是 MainActivity 的一部分。在 OnViewCreated()Fragment.

 private void rebuildGoogleApiClient() {
    // When we build the GoogleApiClient we specify where connected and connection failed
    // callbacks should be returned and which Google APIs our app uses.
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .enableAutoManage(activity, 0 /* clientId */, this)
            .addConnectionCallbacks(this)
            .addApi(Places.GEO_DATA_API)
            .build();
}

堆栈跟踪:

Caused by: java.lang.IllegalStateException: Recursive entry to executePendingTransactions
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1471)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
        at com.google.android.gms.common.api.zzl.zza(Unknown Source)
        at com.google.android.gms.common.api.GoogleApiClient$Builder.zzkL(Unknown Source)
        at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source)
        at com.app.projectpapri.Fragments.LocationScreen.rebuildGoogleApiClient(LocationScreen.java:69)
        at com.app.projectpapri.Fragments.LocationScreen.onViewCreated(LocationScreen.java:93)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:971)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:548)
        at com.app.projectpapri.core.BaseActivity.onStart(BaseActivity.java:38)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1243)

任何帮助将不胜感激。

不要在 onViewCreated() 中调用 rebuildGoogleApiClient(),而是尝试在片段的 onActivityCreated() 方法中调用它。

有关片段生命周期的更多信息,请参见http://developer.android.com/reference/android/app/Fragment.html#Lifecycle

如果仍然不起作用,您可以删除 enableAutoManage(...) 并在 GoogleApiClient 实例上显式调用 connect()disconnect()

详情见https://developers.google.com/places/android/start#connect-client

有帮助吗?

为我工作:

public class TestActivity extends Fragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mGoogleApiClient = new GoogleApiClient.Builder(getActivity())
            .addApi(Places.GEO_DATA_API)
            .build();
}

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

@Override
public void onStop() {
    super.onStop();
    mGoogleApiClient.disconnect();
}
}