android – Fragment crashes on Firebasefirestore snapshot listener-ThrowExceptions

Exception or error:

I have an issue when I create my Home Post Fragment, my app crashes and I could not find any solution.

I think there is something to with this snippet from the log:

at com.social.voteup.Fragments.HomePostsFragment.onViewCreated(HomePostsFragment.java:108)

Here is my full log:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.social.voteup, PID: 23279
    java.lang.IllegalStateException: FragmentManager is already executing transactions
        at androidx.fragment.app.FragmentManagerImpl.ensureExecReady(FragmentManagerImpl.java:1660)
        at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1721)
        at androidx.fragment.app.FragmentManagerImpl.executePendingTransactions(FragmentManagerImpl.java:183)
        at com.google.firebase.firestore.core.ActivityScope.lambda$onFragmentActivityStopCallOnce$1(com.google.firebase:firebase-firestore@@21.4.0:180)
        at com.google.firebase.firestore.core.ActivityScope$$Lambda$2.run(Unknown Source:4)
        at android.app.Activity.runOnUiThread(Activity.java:6923)
        at com.google.firebase.firestore.core.ActivityScope.onFragmentActivityStopCallOnce(com.google.firebase:firebase-firestore@@21.4.0:164)
        at com.google.firebase.firestore.core.ActivityScope.bind(com.google.firebase:firebase-firestore@@21.4.0:192)
        at com.google.firebase.firestore.Query.addSnapshotListenerInternal(com.google.firebase:firebase-firestore@@21.4.0:1054)
        at com.google.firebase.firestore.Query.addSnapshotListener(com.google.firebase:firebase-firestore@@21.4.0:1013)
        at com.google.firebase.firestore.Query.addSnapshotListener(com.google.firebase:firebase-firestore@@21.4.0:957)
        at com.social.voteup.Fragments.HomePostsFragment.onViewCreated(HomePostsFragment.java:108)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:892)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:439)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2079)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1869)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1824)
        at androidx.fragment.app.FragmentManagerImpl.execSingleAction(FragmentManagerImpl.java:1696)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:299)
        at androidx.fragment.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:259)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
        at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:25086)
        at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:735)
        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:481)
        at android.view.View.measure(View.java:25086)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6872)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:25086)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6872)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
        at android.view.View.measure(View.java:25086)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6872)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:742)
        at android.view.View.measure(View.java:25086)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:3083)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1857)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2146)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1745)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7768)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:967)
        at android.view.Choreographer.doCallbacks(Choreographer.java:791)
        at android.view.Choreographer.doFrame(Choreographer.java:726)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:952)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Here is my sample code where the crash happens:

Query firstQuery = firebaseFirestore.collection("Posts")
        .orderBy("timestamp",Query.Direction.DESCENDING).limit(4);

firstQuery.addSnapshotListener(Objects.requireNonNull(getActivity()), (documentSnapshots, e) -> {

    if (e != null) {
        Log.w(TAG, "listen:error", e);
        return;
    }

    assert documentSnapshots != null;
    if (!documentSnapshots.isEmpty()){


        if (isFirstPageFirstLoad){
            lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size()-1);
        }

        for (DocumentChange doc : documentSnapshots.getDocumentChanges()){
            if (doc.getType() == DocumentChange.Type.ADDED){

                String blogPostId = doc.getDocument().getId();

               Post blogPost = doc.getDocument().toObject(Post.class).withId(blogPostId);

                if (isFirstPageFirstLoad){
                    blog_list.add(blogPost);
                    blogRecyclerAdapter.notifyItemInserted(blog_list.size());
                } else {
                    blog_list.add(0,blogPost);
                    blogRecyclerAdapter.notifyItemInserted(0);
                }

            }

        }

        isFirstPageFirstLoad = false;
    }

});

return view;
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *