【问题标题】:java.lang.ClassCastException: java.util.HashMap cannot be cast to custom data classjava.lang.ClassCastException:java.util.HashMap 无法转换为自定义数据类
【发布时间】:2019-07-12 00:59:27
【问题描述】:

我将我的数据类对象上传到 Firestore,它上传正常,当我尝试从另一个活动下载它时,java.lang.ClassCastException 发生。 我需要将 AddToCart.java 中的 Cart 类的对象保存在 Firestore 中(成功)并在 Cart.java 中取回它(失败)。请帮忙!!!!!!

这是我将数据放入 Firestore 的方式:

 private void updateFirestore() {

    Map cartList = new HashMap<String,CartModel>();

    //I put data into the map

    FirebaseFirestore firestoreRef = FirebaseFirestore.getInstance();
    firestoreRef.collection(colloctionKeyTitle)
                  .document(currentSelectedModel.getUserId()).set(cartList)

这里,将具有一个键值的地图上传到firestore:

现在,这里是获取此地图的代码。

 FirebaseFirestore firestoreRef = FirebaseFirestore.getInstance();

    Task<DocumentSnapshot> documentSnapshot = firestoreRef.collection(COLLECTION_ID).document(userId).get()
            .addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    cartList = task.getResult().getData();
                    if (cartList != null) {
                        if (!cartList.isEmpty()) {
                            for (String key : cartList.keySet()) {
                                Object object = cartList.get(key);// In object variable I see my data when debug, but app crashes on next line.
                                Constant.listCartItems.add((CartModel) object); //this is the line where error occur.
                            }
                        }

                    }
                }
            });

这是我的 CartModel.java(数据类)

public class CartModel {
private String productTitle,productPrice, featuredImagePath, quantity;

public CartModel(String productTitle, String productPrice, String featuredImagePath,String quantity) {
    this.productTitle = productTitle;
    this.productPrice = productPrice;
    this.quantity=quantity;
    this.featuredImagePath = featuredImagePath;
}

public CartModel(String productTitle, String productPrice, String featuredImagePath) {
    this.productTitle = productTitle;
    this.productPrice = productPrice;
    this.featuredImagePath = featuredImagePath;
}

public CartModel() {

}


public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getProductTitle() {
    return productTitle;
}

public void setProductTitle(String productTitle) {
    this.productTitle = productTitle;
}

public String getProductPrice() {
    return productPrice;
}

public void setProductPrice(String productPrice) {
    this.productPrice = productPrice;
}

public String getFeaturedImagePath() {
    return featuredImagePath;
}

public void setFeaturedImagePath(String featuredImagePath) {
    this.featuredImagePath = featuredImagePath;
}

}

【问题讨论】:

  • 什么类型的对象给cartList.get(key)
  • 请添加您的CartModel 课程内容并回复@AlexMamo
  • 添加数据类
  • @AlexMamo 请看这个。
  • @AlexMamo 请看这个

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


【解决方案1】:

我现在在您的数据库结构中看到,您在该文档中拥有多个CartModel 类型的对象。要解决此问题,请以 Map 的形式获取数据,如以下代码行所示:

Map<String, Object> map = document.getData();

遍历map 并从中获取所有CartModel 对象。就是这样。

【讨论】:

    【解决方案2】:

    您有两种方法可以解决您的问题:

    解决方案一

    将您的对象转换为正确的对象:

    Object object = (MyObject) cartList.get(key);
                     ^^^^^^^^
    

    解决方案二

    您必须设置 Key 和 Value 类型:

    Map<Key, Value> cartList = new HashMap<String,CartModel>(); 
        ^^^   ^^^^ 
    

    【讨论】:

    • 1> 是多余的演员表
    • 2> 我不控制 cartList 类型,它来自服务器
    • Map 是传入服务器的值的参数。
    【解决方案3】:

    我认为是常量对象的问题。 Constant.listCartItems 与您放入地图的对象不匹配。

    【讨论】:

    • 当我尝试将对象转换为 CartModel 类时发生错误。
    猜你喜欢
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 1970-01-01
    相关资源
    最近更新 更多