Fragment中的GoogleApiClient.Builder.enableAutoManage抛出IllegalStateException:递归进入executePendingTransactions

时间:2015-06-10 07:34:27

标签: android-fragments google-api-client fragment-tab-host android-googleapiclient appcompatactivity

我有AppCompatActivity使用FragmentTabHost有3个标签。其中一个标签使用LocationServices。我希望获得最流畅的用户体验:

如果Android系统中的LocationService已关闭,而用户选择需要Location的标签我想显示AlertDialog让用户打开系统设置中的Location

我有一个应该完成所有这一切的辅助类,它可以在我的应用程序中的其他3个位置工作。在这3个地方,它直接工作"直接"在Activity,但在这个地方,它需要工作"在"标签的Fragment

问题是如果我有这条线:

builder.enableAutoManage(activity, 0, this);

然后builder.build()抛出异常:IllegalStateException: Recursive entry to executePendingTransactions

任何想法如何实现我的目标?

以下是一些相关的代码片段:

public class CityPreferences extends AppCompatActivity {
    private FragmentTabHost mTabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
        mTabHost.addTab(
                mTabHost.newTabSpec("available_cities")
                        .setIndicator(getString(R.string.tab_all_cities))
                , AvailableCityFragment.class, null);
        mTabHost.addTab(
                mTabHost.newTabSpec("nearby_cities")
                        .setIndicator(getString(R.string.tab_nearby_cities))
                , NearbyCityFragment.class, null);
    }
}

NearbyCityFragment我有这一行代码:

class NearbyCityFragment extends Fragment {
...
    LocationServiceHelper.getInstance().startOrDisplayDialog(getActivity());

(我在onAttachonStartonResume

中试用了它

这是我的帮助班'功能:

public class LocationServiceHelper implements
    GoogleApiClient.OnConnectionFailedListener,
    GoogleApiClient.ConnectionCallbacks {

public boolean startOrDisplayDialog(@NonNull final FragmentActivity activity) {
    final boolean servicesConnected = GooglePlayServicesHelper.checkOrDisplayDialog(activity);
    if (servicesConnected) {
        final boolean isEnabled = isLocationEnabledInSystem(activity);
        if (isEnabled) {
            if (null == mGoogleApiClient) {
                mContext = activity;
                mActivity = activity;
                final GoogleApiClient.Builder builder = new GoogleApiClient.Builder(mContext)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(this)
                        .addOnConnectionFailedListener(this);

                // the next line seems to cause the problem:
                builder.enableAutoManage(activity, 0, this);

                mGoogleApiClient = builder
                        .build();
            }
            return start();
        } else {
            final Dialog dialog = getLocationDisabledDialog(activity);
            GooglePlayServicesHelper.showDialog(dialog, activity);
        }
    }
    return false;
}

最后例外:

06-10 10:23:04.831  26725-26725/com.fletech.android.redalert.debug E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.fletech.android.redalert.debug, PID: 26725
    java.lang.IllegalStateException: Recursive entry to executePendingTransactions
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1473)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
            at com.google.android.gms.common.api.g.a(Unknown Source)
            at com.google.android.gms.common.api.GoogleApiClient$Builder.gI(Unknown Source)
            at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source)
            at com.fletech.android.redalert.helper.LocationServiceHelper.startOrDisplayDialog(LocationServiceHelper.java:113)
            at com.fletech.android.redalert.city.NearbyCityFragment.onAttach(NearbyCityFragment.java:44)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:907)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:3)

我相信每次启用自动管理器时都必须使用唯一的clientId。来自documentation

  

clientId - 此客户端的非否定标识符。 在任何给定时间,每个ID只允许一个自动管理的客户端。要重复使用ID,您必须先在之前的客户端上调用stopAutoManage(FragmentActivity)