in my fragment illigleState exception showing in my documentrefernce firesotre collection

Viewed 160

Error showing here in the documentReference line where i used onsnapshotListener.I copied this code into my fragment layout from the MainActivity.In the MainActivity it worked absolutely fine .but when i use it here in my fragment layout it shows error

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

Here's the java code

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;

    }

When I remove this part this project works fine..but also i cannot use the user at that activity.Is there any problem here?

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"));
            }
        });
2 Answers

if you are using firebase and you are listening for changes to document like this

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

            //Doing work
        }
    });

update your code to use ListenerRegistration Instead

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

        }
    });

and remove it once you are done

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

Ok ,,I just solved it.. I just had to remove getActivity() froom my addsnapshotListener

Related