gpt4 book ai didi

android-fragments - 片段中的 GoogleApiClient.Builder.enableAutoManage 抛出 IllegalStateException : Recursive entry to executePendingTransactions

转载 作者:行者123 更新时间:2023-12-04 07:50:25 26 4
gpt4 key购买 nike

我有一个 AppCompatActivity,它有 3 个使用 FragmentTabHost 的选项卡。其中一个选项卡使用 LocationServices。我希望获得尽可能流畅的用户体验:

如果 LocationService 在 android 系统中关闭,并且 仅当 用户选择需要 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 中,我有这 1 行代码:

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

最后是异常(exception):

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)

最佳答案

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

clientId - A non-negative identifier for this client. At any given time, only one auto-managed client is allowed per id. To reuse an id you must first call stopAutoManage(FragmentActivity) on the previous client.

关于android-fragments - 片段中的 GoogleApiClient.Builder.enableAutoManage 抛出 IllegalStateException : Recursive entry to executePendingTransactions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749937/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com