递归进入executePendingTransactions - Android

时间:2014-10-15 19:48:49

标签: java android android-fragments

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

我的代码:

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它正在工作。我该如何解决这个问题?

logcat的:

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)

1 个答案:

答案 0 :(得分:1)

我不知道什么是问题,但是我把try和catch子句放在一边,现在正在运行:

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