我在FragmentManager is already executing transactions
中遇到麻烦。我正在研究如何解决这个问题,但是答案并不是我的代码中的答案。他们使用viewpager和fragment manager,但是我之所以使用fragment事务只是因为我使用的是底部导航栏。
根本原因是来自home片段的快照侦听器。有人可以帮我解决这个问题吗?这是我的代码:
MainActivity.java
final Intent intent = getIntent();
final String UserID = intent.getStringExtra("userid");
final HomeFragment fragment = new HomeFragment();
Bundle bundleh = new Bundle();
bundleh.putString("userid", UserID);
fragment.setArguments(bundleh);
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commitNow();
这是错误所指向的行
HomeFragment.java
db.collection("Baskets").document(uid).collection("Store_Baskets")
.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (e != null){
Log.e(LOG_DB, e.toString());
}else {
List<String> ids = null;
for (DocumentSnapshot ds: queryDocumentSnapshots){
ids.add(ds.getId());
}
if (ids.size()==0){
basket_count.setText("0");
}else {
basket_count.setText(String.valueOf(ids.size()));
}
}
}
});
Stacktrace
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dreamakers.coonna/com.dreamakers.coonna.Activity.HomeBuyersActivity}: java.lang.IllegalStateException: FragmentManager is already executing transactions
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
Caused by: java.lang.IllegalStateException: FragmentManager is already executing transactions
at androidx.fragment.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:2207)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2267)
at androidx.fragment.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:814)
at com.google.firebase.firestore.core.ActivityScope.lambda$onFragmentActivityStopCallOnce$1(com.google.firebase:firebase-firestore@@21.1.1:180)
at com.google.firebase.firestore.core.ActivityScope$$Lambda$2.run(com.google.firebase:firebase-firestore@@21.1.1)
at android.app.Activity.runOnUiThread(Activity.java:5384)
at com.google.firebase.firestore.core.ActivityScope.onFragmentActivityStopCallOnce(com.google.firebase:firebase-firestore@@21.1.1:164)
at com.google.firebase.firestore.core.ActivityScope.bind(com.google.firebase:firebase-firestore@@21.1.1:192)
at com.google.firebase.firestore.Query.addSnapshotListenerInternal(com.google.firebase:firebase-firestore@@21.1.1:1035)
at com.google.firebase.firestore.Query.addSnapshotListener(com.google.firebase:firebase-firestore@@21.1.1:995)
at com.google.firebase.firestore.Query.addSnapshotListener(com.google.firebase:firebase-firestore@@21.1.1:939)
at com.dreamakers.coonna.Activity.HomeFragment.onCreateView(HomeFragment.java:166)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3273)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:620)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1244)
at android.app.Activity.performStart(Activity.java:6108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2608)
at android.app.ActivityThread.access$800(ActivityThread.java:178)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5637)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
答案 0 :(得分:0)
您应将与db.collection相关的代码从HomeFragment的JavaScript
移至onCreate()
,并在片段内使用onActivityCreated()
:
getChildFragmentManager()
此外,您应该使用FragmentTransaction fragmentTransaction =
fragment.getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
而不是fragmentTransaction.commit();
来自Javadoc:
使用
fragmentTransaction.commitNow();
提交的交易可能不会添加到 FragmentManager的后置堆栈交易只能在
commitNow()
之前提交 包含保存其状态的活动。如果尝试提交 之后,将引发异常。这是因为 如果活动需要进行,提交后的状态可能会丢失 从其状态恢复。
答案 1 :(得分:0)
如果您使用的是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();
}