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 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 map1 = (Map) task.getResult().get("users");
String notificationMessage = userName + " has just bought " + itemName + " from " + listName + "'s list!";
UserNotificationsModel userNotificationsModel = new UserNotificationsModel(notificationMessage, userEmail);
for (Map.Entry 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 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;
});
}
当应用程序运行时,在第61行出现致命错误,该行位于for循环“for(Map.Entry entry:map1.entrySet())”
我从Android Studio IDE收到以下错误
E / AndroidRuntime:FATAL EXCEPTION:main进程: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)at example.com.shoppinghelper.shopping_container.ProductContainer $$ Lambda $ 6.onComplete(Unknown来源:0)at com .google.android.gms.tasks.zzf.run(未知来源:23)在android.os.Handler.handleCallback(Handler.java:789)的android.os.Handler.dispatchMessage(Handler.java:98)在android下载位于android.app.ActivityThread.main(ActivityThread.java:6940)的.os.Looper.loop(Looper.java:164)位于com.android.internal.os的java.lang.reflect.Method.invoke(Native Method) .Zygote $ MethodAndArgsCaller.run(Zygote.java:327)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
基本上,代码正在尝试将数据发送到通知集合,前提是某些列表是由某人共享的 . 但是,如果没有人与我分享特定的购物清单,那么我得到上述错误 .
请帮忙 . :)