【问题标题】:Firebase Snapshot Listener: Glide Illegal Argument Exception on Activity Destroyed (Java)Firebase 快照监听器:Glide Illegal Argument Exception on Activity Destroyed (Java)
【发布时间】:2020-02-06 09:14:34
【问题描述】:

我正在适配器上使用 firebase 快照侦听器。并且,每当值更新时,适配器就会更新。但问题是即使活动被破坏,它也会尝试更新。
它在Glide.with(holder.itemView.getContext()) 崩溃,错误是您无法为已破坏的活动开始加载

是的,stackoverflow 上有很多相关的问题。它们中的必须在适配器中是不可能的。我还在我的片段上尝试了recyclerView.setAdapter(null)recyclerView.removeAllViewsInLayout(),但没有一个有效。

简化的适配器代码

 @Override
protected void onBindViewHolder(@NonNull final ChatHolder holder, int position, @NonNull final MessageClass model) {

    final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();


    firebaseFirestore.collection("users").document(model.getUser_receiver())
            .addSnapshotListener(new EventListener<DocumentSnapshot>() {
                @Override
                public void onEvent(@Nullable DocumentSnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
                    if (queryDocumentSnapshots != null) {

                            ProfileClass profileClass = queryDocumentSnapshots.toObject(ProfileClass.class);
                                 String userId = queryDocumentSnapshots.getString("user_uid");

                            if(userId != null) {

                                    if (profileClass.getUser_image().equals("image")) {
                                        Glide.with(holder.itemView.getContext()).load(R.drawable.profile_image).into(holder.roundedImageViewChatsItemChatsImage);

                                    } else {
                                        Glide.with(holder.itemView.getContext()).load(profileClass.getUser_image()).into(holder.roundedImageViewChatsItemChatsImage);

                                    }
                                }
                                }

                        }

            });

日志

Java.lang.IllegalArgumentException: 
at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed 
 (RequestManagerRetriever.java:323)
 at com.bumptech.glide.manager.RequestManagerRetriever.get (RequestManagerRetriever.java:132)
 at com.bumptech.glide.manager.RequestManagerRetriever.get (RequestManagerRetriever.java:116)
 at com.bumptech.glide.Glide.with (Glide.java:707)
 at com.meeti.all.Chats.ChatsFirestore$1.onEvent (ChatsFirestore.java:111)
 at com.meeti.all.Chats.ChatsFirestore$1.onEvent (ChatsFirestore.java:64)
 at com.google.firebase.firestore.DocumentReference.zza (SourceFile:497)
 at com.google.firebase.firestore.zzd.onEvent (Unknown Source:6)
 at com.google.firebase.firestore.g.zzh.zza (SourceFile:28)
 at com.google.firebase.firestore.g.zzi.run (Unknown Source:6)
 at android.os.Handler.handleCallback (Handler.java:873)
 at android.os.Handler.dispatchMessage (Handler.java:99)
 at android.os.Looper.loop (Looper.java:201)
 at android.app.ActivityThread.main (ActivityThread.java:6823)
 at java.lang.reflect.Method.invoke (Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
 at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:873)

我为此苦苦挣扎了一周。
请帮忙。
谢谢

【问题讨论】:

    标签: java android firebase android-recyclerview google-cloud-firestore


    【解决方案1】:

    您从 itemView 请求上下文,但对于异步调用,您应该使用应用上下文,因为在执行该异步调用后视图/片段的上下文可能不可用(在您的情况下)滑动图像加载请求)。

    要解决这个问题,只需替换

    holder.itemView.getContext() 
    

    holder.itemView.getContext().getApplicationContext()
    

    【讨论】:

    • 感谢您的回答。我听说getApplicationContext() 占用了很多资源。这是真的吗?
    • 这可能会有所帮助:Context OR application context?
    【解决方案2】:

    选项 - 1:因为您只需要一次数据,您可以使用get(),如下所示:

    firebaseFirestore
        .collection("users")
        .document(model.getUser_receiver())
        .get()
        .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if(task.isSuccessful()) {
                    DocumentSnapshot documentSnapshot = task.getResult();
    
                    //Rest of your code
                }
            }
        });
    

    选项 - 2: 将您的 context 添加到 addSnapshotListener,如下所示:

    firebaseFirestore
        .collection("users")
        .document(model.getUser_receiver())
        .addSnapshotListener(
            holder.itemView.getContext(), 
            new EventListener<DocumentSnapshot>() 
                {
    
                });
    

    official documentation中所述:

    使用活动范围的侦听器开始侦听此查询。
    在 onStop() 期间监听器将被自动移除。

    【讨论】:

    • 我实际上认为这可能是一个更好的方法。我会检查并告诉你。非常感谢您的回答。
    • @SushanNiroula,这对你有帮助吗?
    猜你喜欢
    • 2019-08-22
    • 1970-01-01
    • 2018-06-09
    • 2022-12-17
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多