所以我试图让购物清单应用程序运行的代码的下面部分,但是我收到一个导致应用程序崩溃的错误。代码:public void setProduct(Context context, View shoppingListViewFragment, String userEmail, String userName, ListModel shoppingListModel, ItemModel productModel) { String listID = shoppingListModel.getListID(); String listName = shoppingListModel.getListName(); String itemID = productModel.getItemID(); String itemName = productModel.getItemName(); Boolean currentList = productModel.getCurrentList(); productNameContainer.setText(itemName); FirebaseFirestore dbRef = FirebaseFirestore.getInstance(); DocumentReference itemIdRef = dbRef.collection("products").document(listID) .collection("shoppingListProducts").document(itemID); itemView.setOnClickListener(view -> { Map<String, Object> map = new HashMap<>(); if (currentList) { map.put("currentList", false); } else { map.put("currentList", true); } itemIdRef.update(map).addOnSuccessListener(aVoid -> { if (currentList) { dbRef.collection("shoppingLists").document(userEmail) .collection("userShoppingLists").document(listID).get().addOnCompleteListener(task -> { Map<String, Object> map1 = (Map<String, Object>) task.getResult().get("users"); String notificationMessage = userName + " has just bought " + itemName + " from " + listName + "'s list!"; } } }); } }); });当应用程序运行时,在第 61 行发生致命错误,该行位于 for 循环“for (Map.Entry entry : map1.entrySet())”基本上,如果某个列表是由某人共享的,代码会尝试将数据发送到通知集合。但是,如果没有人与我共享特定的购物清单,那么我会收到上述错误。我正在关注的视频教程是:https : //www.youtube.com/watch?v=6RzB4HXzQyA&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=16请帮忙。:)
1 回答
慕的地10843
TA贡献1785条经验 获得超8个赞
您的 map1 对象被赋予了 getResult() 的值,但您会发现 getResult() 将返回 null
Map<String, Object> map1 = (Map<String, Object>) task.getResult().get("users");
导致这个 for 语句
for (Map.Entry<String, Object> entry : map1.entrySet()){}
要导致您的空指针异常,您应该在它之前添加一个检查 map1 != null
添加回答
举报
0/150
提交
取消