java - Error when trying to call method - Cannot be applied to ()
问题描述
I have currently created a method for updating a book using Firestore. I am attempting to call this method in onOptionsItemSelected.
I am relatively new to Android and was always under the impression when calling this method I would call it as updateBook(Book book).
Below is my updateBook method
@Override
public void updateBook(Book book) {
String chapterName = editTextChapterName.getText().toString().trim();
String chapterInfo = editTextChapterInfo.getText().toString().trim();
int chapterNumber = numberPickerChapterNumber.getValue();
if (chapterName.trim().isEmpty() || chapterInfo.trim().isEmpty()) { //ensure that user has not left boxes empty
Toast.makeText(this, "Please add a chapter name and the chapter information", Toast.LENGTH_SHORT).show();
}
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference bookRef = db.collection("Book")
.document();
bookRef.update("chapterName", book.getChapterName(),
"chapterInfo", book.getChapterInfo(),"chapterNumber", book.getChapterNumber())
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
Toast.makeText(AdminUpdateActivity.this, "Success", Toast.LENGTH_SHORT).show();
}
}
});
Below is where I am trying to call my method
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_icon:
updateBook();
return true;
default:
return super.onOptionsItemSelected(item);
}
Within the updateBook parenthesis I am getting an error "Cannot be applied to ()".
I have read through other questions on here to do with this topic but have not found a solution. Can someone explain how to fix this issue and why?
Thanks
解决方案
要解决此问题,您应该更改以下代码行:
DocumentReference bookRef = db.collection("Book")
.document();
至
DocumentReference bookRef = db.collection("Book")
.document(bookId);
如果不将书籍的 id 传递给document()
方法,Firebase SDK 并不真正知道您要更新哪个书籍对象。除此之外,在它上调用document()
方法DocumentReference
每次只生成一个新的随机 id。
还需要进行另一项更改。updateBook()
还请从您的方法中删除参数。应该:
public void updateBook() {}
没有任何争论。
推荐阅读
- typescript - 打字稿,将一个对象映射到另一个对象?
- visual-studio-code - VS Code 是否有扩展来替换标题栏?
- python - 请求错误:未找到连接适配器
- python - 如何使用与功能相关的颜色图绘制立方体或平行六面体
- python - 如何使用 django_oauth_toolkit 从 refresh_token 获取新的 access_token?
- filter - 标准外设库中的 STM8A CAN 过滤
- javascript - 点击html标签后Javascript不起作用?
- php - 刷新页面时如何自动清除缓存?
- azure - 接收双更新通知的 Azure IoT 设备
- excel - 查找包含波浪号的文本