gpt4 book ai didi

java - 递归进入executePendingTransactions - Android

转载 作者:行者123 更新时间:2023-11-30 02:36:57 26 4
gpt4 key购买 nike

我想从一个 fragment 转到另一个 fragment 。首先我要传递一些数据,然后我要刷新我的适配器,至少我要转到新的 fragment 。

我的代码:

public class TestFragment extends Fragment {

private String horoscope_sign;

private Bundle bundle;

private FragmentItemHoroscopePage itemHoroscope;
private FragmentManager manager;
private FragmentTransaction transaction;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_fragment, container, false);

horoscope_sign = getArguments().getString("horoscope_sign");

bundle = new Bundle();
itemHoroscope = new FragmentItemHoroscopePage();
manager = getFragmentManager();

// Resetting adapter
if(FragmentItemHoroscopePage.getAdapter() != null) {
FragmentItemHoroscopePage.getAdapter().notifyDataSetChanged();
}

// Going to another fragment
transaction = manager.beginTransaction();
bundle.putString("horoscope_sign", horoscope_sign);
itemHoroscope.setArguments(bundle);
transaction.replace(R.id.mainContainer, itemHoroscope, "ItemHoroscope");
transaction.addToBackStack("ItemHoroscope");
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.commit();

return view;
}
}

我在检查适配器时遇到错误,我不知道为什么。有趣的是,如果我将整个代码放在 OnButtonClick 中,它就会工作。我该如何解决这个问题??

日志:

FATAL EXCEPTION: main
10-15 21:40:01.050:
java.lang.IllegalStateException: Recursive entry to executePendingTransactions
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1456)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:897)
at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2824)
at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
at com.kiko.bmgu.crnobelo.horoscope.TestFragment.onCreateView(TestFragment.java:39)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4929)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:798)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
at dalvik.system.NativeStart.main(Native Method)

最佳答案

我不知道有什么问题,但我把 try 和 catch 子句放在一起,它现在可以工作了:

try { 
if(FragmentItemHoroscopePage.getAdapter() != null) {
System.out.println("REFRESH");
FragmentItemHoroscopePage.getAdapter().notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
}

关于java - 递归进入executePendingTransactions - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26390858/

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