gpt4 book ai didi

java - 在我的 fragment 中,非法状态异常显示在我的文档引用 firestore 集合中

转载 作者:行者123 更新时间:2023-12-02 14:58:05 25 4
gpt4 key购买 nike

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

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;

}

当我删除这部分时,该项目工作正常。但我也无法在该 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();
}

关于java - 在我的 fragment 中,非法状态异常显示在我的文档引用 firestore 集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60943736/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com