Hy Guys
因此,我试图在购物清单应用程序的代码下面获取此代码,但我遇到一个导致应用程序崩溃的错误。
代码:
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!";
UserNotificationsModel userNotificationsModel = new UserNotificationsModel(notificationMessage, userEmail);
for (Map.Entry<String, Object> entry : map1.entrySet()) {
String sharedUserEmail = entry.getKey();
if (!sharedUserEmail.equals(userEmail)) {
dbRef.collection("notifications").document(sharedUserEmail)
.collection("userNotifications").document()
.set(userNotificationsModel);
}
}
});
}
});
});
itemView.setOnLongClickListener(view -> {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Edit / Delete Item");
EditText editText = new EditText(context);
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_CAP_WORDS);
editText.setText(itemName);
editText.setSelection(editText.getText().length());
editText.setHint("Please type item name");
editText.setHintTextColor(Color.GRAY);
builder.setView(editText);
builder.setPositiveButton("Update", (dialogInterface, i) -> {
String newItemName = editText.getText().toString().trim();
Map<String, Object> map = new HashMap<>();
map.put("itemName", newItemName);
itemIdRef.update(map);
});
builder.setNegativeButton("Delete", (dialogInterface, i) -> {
itemIdRef.delete().addOnSuccessListener(aVoid -> Snackbar.make(shoppingListViewFragment, "Item deleted!", Snackbar.LENGTH_LONG).show());
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
return true;
});
}
运行应用程序时,在for循环“ for(Map.Entry entry:map1.entrySet())”的第61行发生致命错误
我从Android Studio IDE中收到以下错误
E / Android运行时:致命异常:主要 流程:example.com.shoppinghelper,PID:17150 java.lang.NullPointerException:尝试在null上调用接口方法'java.util.Set java.util.Map.entrySet()' 对象参考 在example.com.shoppinghelper.shopping_container.ProductContainer.lambda $ null $ 0 $ ProductContainer(ProductContainer.java:61) 在example.com.shoppinghelper.shopping_container.ProductContainer $$ Lambda $ 6.onComplete(未知 来源:0) 在com.google.android.gms.tasks.zzf.run(未知来源:23) 在android.os.Handler.handleCallback(Handler.java:789) 在android.os.Handler.dispatchMessage(Handler.java:98) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6940) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:327) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
基本上,如果特定列表是由某人共享的,则代码尝试将数据存储到通知集合中。但是,如果没有人与我共享特定的购物清单,那么我将收到上述错误。
我关注的视频教程是: https://www.youtube.com/watch?v=6RzB4HXzQyA&list=PLn2n4GESV0AmXOWOam729bC47v0d0Ohee&index=16
请帮助。 :)
答案 0 :(得分:0)
您的map1对象获得了getResult()的值,但您会发现getResult()将返回null
NULL
这是出于声明
Map<String, Object> map1 = (Map<String, Object>) task.getResult().get("users");
要导致空指针异常,您应该在它之前添加一个检查map1!= null