更改片段会导致崩溃。 FragmentManager已经在执行交易

时间:2018-09-04 14:41:44

标签: android transactions fragment

我有一个片段(PokedexFragment.java),其中包含带有一些项目的GridView(通过Custom Recyclerview)。当我单击一个项目时,当前片段将被另一个片段(SinglePokemonFragment.java)替换为所选项目的详细信息。用户想直接转到gridview中的最后一项或下一项(按“前进” /“后​​退”按钮)。当他按下硬件“后退按钮”时,应用程序崩溃,错误为“ FragmentManager已在执行事务!”

它在(Logcat的(SinglePokemonFragment.java:391)崩溃了

fragmentManager.popBackStackImmediate(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);

在performNoBackStackTransaction()

代码的哪一部分是错误的或必须更新,为什么?

SinglePokemonFragment.java(按钮)

cLayBack.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        Fragment newFragment = new SinglePokemonFragment();
        performNoBackStackTransaction(getFragmentManager(), "", newFragment, 1);

    }
});

cLayForward.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        // Create new fragment and transaction
        Fragment newFragment = new SinglePokemonFragment();
        performNoBackStackTransaction(getFragmentManager(), "", newFragment, -1);
    }
});

public void performNoBackStackTransaction(final FragmentManager fragmentManager, String tag, Fragment fragment, int id) {
    final int newBackStackLength = fragmentManager.getBackStackEntryCount() +1;

    Bundle bundle = new Bundle();

    bundle.putInt("POKEMON_ID", Integer.parseInt(db.getPokemonID(intPokeDexID-id)));
    bundle.putInt("ARRAY_ID", intPokeArrayID-id);
    bundle.putInt("POKEMON_ID_MAX", getIntPokemonIDMax);
    bundle.putInt("POKEDEX_ID", intPokeDexID-id);
    fragment.setArguments(bundle);

    fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment, tag)
            .addToBackStack(tag)
            .commitNow();

    fragmentManager.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            int nowCount = fragmentManager.getBackStackEntryCount();
            if (newBackStackLength != nowCount) {
                // we don't really care if going back or forward. we already performed the logic here.
                fragmentManager.removeOnBackStackChangedListener(this);

                if ( newBackStackLength > nowCount ) { // user pressed back
                    fragmentManager.popBackStackImmediate(0, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                }
            }
        }
    });
}

MainFragment.java(带有ViewPager)

public class MainFragment extends Fragment {
    private TextView titleTxtView;
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    TabLayout tabLayout;
    ViewPager viewPager;
    private FragmentActivity myContext;

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

        tabLayout = (TabLayout) view.findViewById(R.id.tab_layout);
        viewPager = (ViewPager) view.findViewById(R.id.pager);

        if (viewPager != null) {
            Log.v("ViewPager", "IS NOT NULL");
            setupViewPager(viewPager);
            tabLayout.getTabAt(0).setIcon(R.drawable.ic_pokedex);
            tabLayout.getTabAt(1).setIcon(R.drawable.pokedex_kanto);
            tabLayout.getTabAt(2).setIcon(R.drawable.pokedex_johto);
            tabLayout.getTabAt(3).setIcon(R.drawable.pokedex_hoenn);
        }else{
            Log.v("ViewPager", "ISNULL");
        }



        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        myContext=(FragmentActivity) activity;
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    private void setupViewPager(ViewPager viewPager) {

        PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(), getActivity());
        adapter.addFragment(new PokedexFragment().newInstance(0, 9999), "Alle");
        adapter.addFragment(new PokedexFragment().newInstance(1, 151), "Kanto");
        adapter.addFragment(new PokedexFragment().newInstance(152, 251), "Johto");
        adapter.addFragment(new PokedexFragment().newInstance(252, 386), "Hoenn");

        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
    }

}

PokedexFragment.java

package com.spicysoftware.goexpert;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toolbar;

import java.util.ArrayList;
import java.util.List;

import static android.content.ContentValues.TAG;

public class PokedexFragment extends Fragment{

    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    MySQLiteHelper db;

    // TODO: Rename and change types of parameters
    private int param1, param2;

    public PokedexFragment() {
        // Required empty public constructor

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            param1 = getArguments().getInt(ARG_PARAM1);
            param2 = getArguments().getInt(ARG_PARAM2);
        }
    }

    public static PokedexFragment newInstance(int intDexFrom, int intDexTo) {
        PokedexFragment fragment = new PokedexFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, intDexFrom);
        args.putInt(ARG_PARAM2, intDexTo);
        fragment.setArguments(args);
        Log.d(TAG, "newInstance: 2F");
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        View myView = inflater.inflate(R.layout.fragment_pokedexall, container, false);
        db = new MySQLiteHelper(getActivity());

        RecyclerView recyclerView = (RecyclerView)myView.findViewById(R.id.rvPokedex);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getActivity(),4);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setItemViewCacheSize(20);
        recyclerView.setDrawingCacheEnabled(true);
        recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);

        CustomRecyclerAdapterPokedexGrid adapter = new CustomRecyclerAdapterPokedexGrid(getActivity(), db.getPokemon(param1,param2));
        recyclerView.setAdapter(adapter);

        return myView;
    }

}

CustomRecyclerAdapterPokedexGrid.java(ItemClickListener)

viewHolder.setClickListener(new ItemClickListener() {
            @Override
            public void onClick(View view, int position, boolean isLongClick) {

                if (isLongClick) {

                } else {

                    String test = strPokemonInfo.get(i);
                    String[] separated = test.split("/");
                    intPokemonID = Integer.parseInt(separated[0]);
                    pokeDexID = Integer.parseInt(separated[1]);

                    Fragment myFragment = new SinglePokemonFragment();
                    Bundle bundle = new Bundle();

                    bundle.putInt("POKEMON_ID", intPokemonID);
                    bundle.putInt("ARRAY_ID", i);
                    bundle.putInt("POKEMON_ID_MAX", strPokemonInfo.size());
                    bundle.putInt("POKEDEX_ID", pokeDexID);

                    myFragment.setArguments(bundle);
                    AppCompatActivity activity = (AppCompatActivity) view.getContext();
                    activity.getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, myFragment).addToBackStack(null).commit();

                }

            }
        });

Logcat

09-04 14:34:23.791 5296-5296/com.spicysoftware.goexpert E/InputEventSender: Exception dispatching finished signal.
09-04 14:34:23.792 5296-5296/com.spicysoftware.goexpert E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
    java.lang.IllegalStateException: FragmentManager is already executing transactions
        at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:2177)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2233)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:819)
        at com.spicysoftware.goexpert.SinglePokemonFragment$6.onBackStackChanged(SinglePokemonFragment.java:391)
        at android.support.v4.app.FragmentManagerImpl.reportBackStackChanged(FragmentManager.java:2707)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2405)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:851)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:794)
        at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:181)
        at com.spicysoftware.goexpert.MainActivity.onBackPressed(MainActivity.java:173)
        at android.app.Activity.onKeyUp(Activity.java:2725)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2700)
        at android.app.Activity.dispatchKeyEvent(Activity.java:3026)
        at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:317)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4327)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4298)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4028)
        at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4189)
        at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2365)
        at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1961)
        at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1952)
        at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2342)
        at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:323)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
09-04 14:34:23.792 5296-5296/com.spicysoftware.goexpert D/AndroidRuntime: Shutting down VM
09-04 14:34:23.793 5296-5296/com.spicysoftware.goexpert E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.spicysoftware.goexpert, PID: 5296
    java.lang.IllegalStateException: FragmentManager is already executing transactions
        at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:2177)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2233)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:819)
        at com.spicysoftware.goexpert.SinglePokemonFragment$6.onBackStackChanged(SinglePokemonFragment.java:391)
        at android.support.v4.app.FragmentManagerImpl.reportBackStackChanged(FragmentManager.java:2707)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2405)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:851)
        at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:794)
        at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:181)
        at com.spicysoftware.goexpert.MainActivity.onBackPressed(MainActivity.java:173)
        at android.app.Activity.onKeyUp(Activity.java:2725)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2700)
        at android.app.Activity.dispatchKeyEvent(Activity.java:3026)
        at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
        at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:316)
        at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:58)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:317)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4327)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4298)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4028)
        at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4189)
        at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2365)
        at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1961)
        at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1952)
        at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2342)
        at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:323)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:6077)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

0 个答案:

没有答案