在Fragment中使用GoogleApiClient递归进入executePendingTransactions

时间:2015-05-05 14:22:00

标签: android android-fragments google-places-api google-api-client

我正在尝试在我的应用中使用Place Autocomplete 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)

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:6)

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

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

如果仍然不起作用,您可以删除enableAutoManage(...)并在connect()实例上明确致电disconnect()GoogleApiClient

有关详细信息,请参阅https://developers.google.com/places/android/start#connect-client

这有帮助吗?

答案 1 :(得分:0)

为我工作:

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