尝试调用无法应用于()的方法时出现java错误
我目前已经创建了一个使用Firestore更新书籍的方法。我试图在OnOptions ItemSelected中调用此方法
我对安卓系统比较陌生,在调用这个方法时总是有这样的印象,我称之为updateBook(书籍)
下面是我的updateBook方法
@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();
}
}
});
下面是我试图调用我的方法的地方
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.save_icon:
updateBook();
return true;
default:
return super.onOptionsItemSelected(item);
}
在updateBook括号内,我得到一个错误“不能应用于()”
我已经通读了关于这个主题的其他问题,但没有找到解决方案。有人能解释一下如何解决这个问题以及原因吗
谢谢
# 1 楼答案
要解决这个问题,您应该更改以下代码行:
到
如果不将书的id传递给
document()
方法,Firebase SDK实际上不知道要更新哪个书对象。除此之外,对DocumentReference
调用document()
方法时,每次只会生成一个新的随机id还有另一个变化也是需要的。还请从
updateBook()
方法中删除参数。应该是:没有任何争论