私のフラグメントilligleState例外で私documentrefernce firesotreコレクションに示します

DRITAラーマン:

ここで私が使用documentReferenceラインで示すエラーonsnapshotListener.Iから私のフラグメントのレイアウトにこのコードをコピーしたMainActivity.IN MainActivity私はここに私のフラグメントでそれを使用する場合、それは絶対に罰金.but働いていたが、それはエラーを示していレイアウト

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$1(com.google.firebase:firebase-firestore@@21.4.1:180)
        at com.google.firebase.firestore.core.ActivityScope$$Lambda$2.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;

    }

私はこの部分を削除すると、このプロジェクトはfine..butも動作します、私はその時にユーザーを使用することはできませんactivityここに.Isそこに何か問題が?

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();
}

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=375021&siteId=1