You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Firestore文档引用段数异常求助:RecyclerView适配器获取关注用户帖子描述时应用崩溃

解决Firestore文档引用错误并展示关注用户帖子的Description

首先,咱们来拆解你遇到的错误:

java.lang.IllegalArgumentException: Invalid document reference. Document references must have an even number of segments, but post has 1

这个错误的核心是Firestore的文档引用格式要求严格:文档路径必须是「集合/文档」成对出现的结构,总段数是偶数(比如collection/doc是2段,collection/doc/subcollection/subdoc是4段)。你代码里的document("/post")只有1个路径段(post),显然不符合要求——因为post应该是一个集合,而非文档。

修正步骤

1. 修复错误的路径引用

把你代码里错误的document("/post")改成collection("post"),因为post是根集合,不是单个文档。

2. 根据实际数据结构调整查询

场景1:帖子存储在用户专属子集合中

假设你的数据结构是「每个用户的帖子存储在post集合下的用户ID文档的userPosts子集合中」,修正后的查询代码如下:

@Override
protected void onBindViewHolder(@NonNull FindFriendsViewHolder findFriendsViewHolder, int i, @NonNull GetInfoPost getInfoPost) {
    final String listUserId = getSnapshots().getSnapshot(i).getId();
    
    // 修正后的Firestore查询
    FirebaseFirestore.getInstance()
        .collection("post") // 访问根集合post
        .document(listUserId) // 用户ID作为文档ID
        .collection("userPosts") // 用户的帖子子集合
        .limit(1) // 示例:取该用户的第一条帖子(可根据需求调整)
        .get()
        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
            @Override
            public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
                if (!queryDocumentSnapshots.isEmpty()) {
                    DocumentSnapshot postDoc = queryDocumentSnapshots.getDocuments().get(0);
                    String description = postDoc.getString("description");
                    findFriendsViewHolder.txtDescrizione.setText(description);
                }
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                // 处理查询失败的情况,比如显示默认文本或Toast提示
                findFriendsViewHolder.txtDescrizione.setText("加载失败");
            }
        });
}

场景2:帖子信息直接存在用户文档字段中

如果你的数据结构是「用户帖子信息直接存在post集合下的用户ID文档的字段中」(比如userPosts是一个包含description的Map字段),那代码可以改成这样:

@Override
protected void onBindViewHolder(@NonNull FindFriendsViewHolder findFriendsViewHolder, int i, @NonNull GetInfoPost getInfoPost) {
    final String listUserId = getSnapshots().getSnapshot(i).getId();
    
    FirebaseFirestore.getInstance()
        .collection("post")
        .document(listUserId)
        .get()
        .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                // 直接从文档字段中获取description
                String description = documentSnapshot.getString("userPosts.description");
                findFriendsViewHolder.txtDescrizione.setText(description != null ? description : "暂无描述");
            }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                findFriendsViewHolder.txtDescrizione.setText("加载失败");
            }
        });
}

3. 额外优化建议

  • 避免频繁请求:在onBindViewHolder中每次绑定都发起Firestore请求会导致大量重复调用,建议考虑在GetInfoPost模型中预先关联帖子数据,或者使用Firestore的实时监听(addSnapshotListener)来缓存数据。
  • 空值处理:务必处理description为空的情况,避免NullPointerException。

内容的提问来源于stack exchange,提问作者Conta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 19:27:33