在我的文档参考 firestore 集合中显示的片段 illegalStateexception

in my fragment illigleState exception showing in my documentrefernce firesotre collection

在我使用 onsnapshotListener 的 documentReference 行中显示错误。我将此代码从 MainActivity 复制到我的片段布局中。在 MainActivity 中它工作得非常好。但是当我在我的片段布局中使用它时,它显示错误

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myeducationapp, PID: 7596
    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(com.google.firebase:firebase-firestore@@21.4.1:180)
        at com.google.firebase.firestore.core.ActivityScope$$Lambda.run(Unknown Source:4)
        at android.app.Activity.runOnUiThread(Activity.java:6282)
        at com.google.firebase.firestore.core.ActivityScope.onFragmentActivityStopCallOnce(com.google.firebase:firebase-firestore@@21.4.1:164)
        at com.google.firebase.firestore.core.ActivityScope.bind(com.google.firebase:firebase-firestore@@21.4.1:192)
        at com.google.firebase.firestore.DocumentReference.addSnapshotListenerInternal(com.google.firebase:firebase-firestore@@21.4.1:514)
        at com.google.firebase.firestore.DocumentReference.addSnapshotListener(com.google.firebase:firebase-firestore@@21.4.1:456)
        at com.google.firebase.firestore.DocumentReference.addSnapshotListener(com.google.firebase:firebase-firestore@@21.4.1:397)
        at com.example.myeducationapp.homeScreen.onCreateView(homeScreen.java:50)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        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.execPendingActions(FragmentManagerImpl.java:1727)
        at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
        at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
        at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
        at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1391)
        at android.app.Activity.performStart(Activity.java:7157)
        at android.app.ActivityThread.handleStartActivity(ActivityThread.java:2980)
        at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:180)
        at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:165)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:142)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:70)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1843)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6758)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:497)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:912)
I/Process: Sending signal. PID: 7596 SIG: 9

这是 java 代码

package com.example.myeducationapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;

import java.util.ArrayList;

import de.hdodenhof.circleimageview.CircleImageView;

public class homeScreen extends Fragment {
    private ArrayList<Course> arrayList;
    private RecyclerView recyclerCourse;
    private FragmentAListener listener;
    CircleImageView circleImageView;
    TextView name;
    FirebaseAuth FAuth;
    FirebaseFirestore FStore;
    String userId;
    public interface FragmentAListener{

    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home_screen,container,false);
        circleImageView = v.findViewById(R.id.imageuser);
        name = v.findViewById(R.id.name);
        FAuth = FirebaseAuth.getInstance();
        FStore = FirebaseFirestore.getInstance();
        userId = FAuth.getCurrentUser().getUid();
        DocumentReference documentReference = FStore.collection("users").document(userId);
        documentReference.addSnapshotListener(getActivity(), new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                name.setText(documentSnapshot.getString("FName"));
            }
        });
        circleImageView.setOnClickListener(new View.OnClickListener() {
            private static final String TAG ="TAG" ;

            @Override
            public void onClick(View v) {
                Intent i = new Intent(getActivity(),Myprofile.class);
                startActivity(i);
            }
        });
        recyclerCourse = v.findViewById(R.id.recylclerViewCourse);
        CourseData();
        showRecyclerView();

        return v;

    }

当我删除这部分时,这个项目工作正常..但我也无法使用那个用户activity。这里有什么问题吗?

DocumentReference documentReference = FStore.collection("users").document(userId);
        documentReference.addSnapshotListener(getActivity(), new EventListener<DocumentSnapshot>() {
            @Override
            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                name.setText(documentSnapshot.getString("FName"));
            }
        });

如果您使用的是 firebase 并且正在侦听对文档的更改,就像这样

 docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {

            //Doing work
        }
    });

更新您的代码以改为使用 ListenerRegistration

  ListenerRegistration registration = docRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
        @Override
        public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {

        }
    });

完成后将其删除

   @Override
public void onStop() {
    super.onStop();
    registration.remove();
}

好的,我刚刚解决了它。我只需要从我的 addsnapshotListener

中删除 getActivity()