如何修复“ FragmentManager已在执行事务”错误?

时间:2019-05-28 23:13:33

标签: java android google-cloud-firestore fragmenttransaction fragment-lifecycle

自从我更新了应用程序实施以来,我发现自己无法吃午餐。

我收到“ java.lang.IllegalStateException:FragmentManager已经在执行事务”错误,并且似乎找不到解决此问题的方法...

public class MainActivity extends AppCompatActivity {

private Toolbar mainToolbar;
private FirebaseAuth mAuth;
private FirebaseFirestore firebaseFirestore;
private String current_user_id;
private BottomNavigationView mainbottomNav;
private HomeFragment accountFragment;
private NotificationFragment notificationFragment;
private FilterFragment filterFragment;
private UserProfileFragment userProfileFragment;
private MapFragment homeFragment;
private static final String TAG = "MainActivity";



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAuth = FirebaseAuth.getInstance();
    firebaseFirestore = FirebaseFirestore.getInstance();


    if (mAuth.getCurrentUser() != null) {
        current_user_id = mAuth.getCurrentUser().getUid();
        mainbottomNav = findViewById(R.id.mainBottomNav);
        BottomNavigationViewHelper.disableShiftMode(mainbottomNav);


        BottomNavigationMenuView bottomNavigationMenuView =
                (BottomNavigationMenuView) mainbottomNav.getChildAt(0);
        View v = bottomNavigationMenuView.getChildAt(2);
        BottomNavigationItemView itemView = (BottomNavigationItemView) v;



        // FRAGMENTS
        homeFragment = new MapFragment();
        notificationFragment = new NotificationFragment();
        accountFragment = new HomeFragment();
        filterFragment = new FilterFragment();
        userProfileFragment = new UserProfileFragment();


        initializeFragment();


        mainbottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.main_container);

                switch (item.getItemId()) {

                    case R.id.bottom_action_home:

                        replaceFragment(homeFragment, currentFragment);
                        return true;


                    case R.id.bottom_action_filters:
                        replaceFragment(filterFragment, currentFragment);
                        return true;

                    case R.id.bottom_action_account:
                        replaceFragment(userProfileFragment, currentFragment);
                        replaceFragment(accountFragment, currentFragment);
                        return true;

                    case R.id.bottom_action_notif:

                        replaceFragment(notificationFragment, currentFragment);
                        notifCount.setVisibility(View.GONE);

                        return true;

                    default:
                        return false;


                }

            }
        });



private void initializeFragment() {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();

    fragmentTransaction.add(R.id.main_container, homeFragment);
    fragmentTransaction.add(R.id.main_container, filterFragment);
    fragmentTransaction.add(R.id.main_container, notificationFragment);
    fragmentTransaction.add(R.id.main_container, accountFragment);
    fragmentTransaction.add(R.id.main_container, userProfileFragment);


    fragmentTransaction.hide(filterFragment);
    fragmentTransaction.hide(notificationFragment);
    fragmentTransaction.hide(accountFragment);
    fragmentTransaction.hide(userProfileFragment);


    fragmentTransaction.commit();


}

@SuppressLint("RestrictedApi")
private void replaceFragment(Fragment currentFragment, Fragment fragment) {

    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    if (currentFragment == homeFragment) {

        fragmentTransaction.hide(userProfileFragment);
        fragmentTransaction.hide(filterFragment);
        fragmentTransaction.hide(accountFragment);
        fragmentTransaction.hide(notificationFragment);

        if (addEventBtn.isEnabled()) {
            addEventBtn.setVisibility(View.VISIBLE);
        }
        addPostBtn.setEnabled(false);
    }

    if (currentFragment == accountFragment) {

        fragmentTransaction.hide(filterFragment);
        fragmentTransaction.hide(homeFragment);
        fragmentTransaction.hide(notificationFragment);


        addPostBtn.setEnabled(true);

        if (addEventBtn.isEnabled()) {
            addEventBtn.setVisibility(View.GONE);
        }
    }

    if (currentFragment == notificationFragment) {


        fragmentTransaction.hide(userProfileFragment);
        fragmentTransaction.hide(filterFragment);
        fragmentTransaction.hide(homeFragment);
        fragmentTransaction.hide(accountFragment);

        addPostBtn.setEnabled(false);
        if (addEventBtn.isEnabled()) {
            addEventBtn.setVisibility(View.GONE);
        }

    }
    if (currentFragment == filterFragment) {

        fragmentTransaction.hide(userProfileFragment);
        fragmentTransaction.hide(notificationFragment);
        fragmentTransaction.hide(homeFragment);
        fragmentTransaction.hide(accountFragment);

        addPostBtn.setEnabled(false);
        if (addEventBtn.isEnabled()) {
            addEventBtn.setVisibility(View.GONE);
        }

    }
    fragmentTransaction.show(currentFragment);
    fragmentTransaction.commit();

    //fragmentTransaction.replace(R.id.main_container, fragment);


}
}

错误:

java.lang.RuntimeException:无法启动活动ComponentInfo {com.app.Activities.MainActivity}:java.lang.IllegalStateException:FragmentManager已在执行事务

编辑2:经过进一步研究,我能够找到实际上导致崩溃的原因。显然,每当我在片段上使用“ addSnapshotListener”时,都会出现“ FragmentManager已在执行事务”错误。我认为它与“ getActivity()”有关,但是,我无法理解为什么它提示应用程序崩溃。我正在按照Firebase文档中的建议分离侦听器。

片段代码:

public class NotificationFragment extends Fragment {

private RecyclerView mNotificationList;
private NotificationsAdapter notificationsAdapter;
private List<Notifications> mNotifList;
private FirebaseFirestore mFirestore;
private String TAG = "Notification Fragment";


public NotificationFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View v = inflater.inflate(R.layout.fragment_notification, container, false);


    mNotifList = new ArrayList<>();

    mNotificationList = (RecyclerView) v.findViewById(R.id.notification_list);
    notificationsAdapter = new NotificationsAdapter(mNotifList);


    mNotificationList.setHasFixedSize(true);
    mNotificationList.setLayoutManager(new LinearLayoutManager(container.getContext()));
    mNotificationList.setAdapter(notificationsAdapter);

    mFirestore = FirebaseFirestore.getInstance();

    final String current_user_id = FirebaseAuth.getInstance().getCurrentUser().getUid();
    Query query = mFirestore.collection("Users").document(current_user_id).collection("Notifications");
    ListenerRegistration registration = query.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
            if (documentSnapshots != null && !documentSnapshots.isEmpty()) {

                for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
                    if (doc.getType() == DocumentChange.Type.ADDED) {
                        final String notificationId = doc.getDocument().getId();
                        Notifications notifications = doc.getDocument().toObject(Notifications.class).withId(notificationId);
                        mNotifList.add(notifications);
                        Log.d(TAG, "list ids: " + notificationId);
                        notificationsAdapter.notifyDataSetChanged();


                    }
                }

            }
        }
    });
    registration.remove();
    return v;
}

0 个答案:

没有答案