尝试将书籍保存到数据库时发生以下异常:
我的代码(从此处删除了默认导入):
public class AddBookActivity extends AppCompatActivity {
所有变量都在这里初始化。.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_book);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getCurrentUser().getUid();
current_user_id = mAuth.getUid();
BookReference = FirebaseStorage.getInstance().getReference();
userRef = FirebaseDatabase.getInstance().getReference();
bookRef = FirebaseDatabase.getInstance().getReference().child("Books");
selectBookImage = (ImageButton) findViewById(R.id.addimageButton);
addBookButton = (Button) findViewById(R.id.buttonAddBook);
bookTitle = (EditText) findViewById(R.id.addbookTitle);
bookDescription = (EditText) findViewById(R.id.addbookDescription);
bookAuthor = (EditText) findViewById(R.id.addbookAuthor);
loadingBar = new ProgressDialog(this);
mToolBar = (Toolbar) findViewById(R.id.add_book_toolbar);
setSupportActionBar(mToolBar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle("წიგნის დამატება");
selectBookImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OpenGallery();
}
});
addBookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ValidateBookInfo();
}
});
}
private void ValidateBookInfo() {
Author = bookAuthor.getText().toString();
Description = bookDescription.getText().toString();
Title = bookTitle.getText().toString();
if (ImageUrl == null){
Toast.makeText(this,"წიგნის სურათი არ შეიძლება იყოს ცარიელი",Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(Author)){
Toast.makeText(this,"წიგნის სათაური არ შეიძლება იყოს ცარიელი",Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(Description)){
Toast.makeText(this,"წიგნის აღწერა არ შეიძლება იყოს ცარიელი",Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(Title)){
Toast.makeText(this,"წიგნის ავტორი არ შეიძლება იყოს ცარიელი",Toast.LENGTH_SHORT).show();
}
else{
loadingBar.setTitle("ახალი წიგნის დამატება");
loadingBar.setMessage("გთხოვთ დაელოდეთ.. წიგნი იტვირთება..");
loadingBar.show();
loadingBar.setCanceledOnTouchOutside(true);
StoringImageToStorage();
}
}
private void StoringImageToStorage() {
Calendar callForDate = Calendar.getInstance();
SimpleDateFormat currentDate = new SimpleDateFormat("dd-MMMM-yyyy");
saveCurrentDate = currentDate.format(callForDate.getTime());
Calendar callForTime = Calendar.getInstance();
SimpleDateFormat currentTime = new SimpleDateFormat("HH:mm");
saveCurrentTime = currentTime.format(callForTime.getTime());
bookRandomName = saveCurrentDate + saveCurrentTime;
StorageReference filePath = BookReference.child("Book Images").child(ImageUrl.getLastPathSegment() + bookRandomName + ".jpg");
filePath.putFile(ImageUrl).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()){
DownloadURL = task.getResult().toString();
Toast.makeText(AddBookActivity.this,"წიგნის სურათი წარმატებით აიტვირთა",Toast.LENGTH_SHORT).show();
SaveBookToDatabase();
}else{
String message = task.getException().getMessage();
Toast.makeText(AddBookActivity.this,"დაფიქსირდა შეცდომა " + message,Toast.LENGTH_SHORT).show();
}
}
});
}
private void SaveBookToDatabase() {
userRef.child(current_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
Integer BookLikes = 0;
HashMap booksMap = new HashMap();
booksMap.put("uid",current_user_id);
booksMap.put("date",saveCurrentDate);
booksMap.put("time",saveCurrentTime);
booksMap.put("title",bookTitle);
booksMap.put("description",bookDescription);
booksMap.put("author",bookAuthor);
booksMap.put("booksimage",DownloadURL);
booksMap.put("likes",BookLikes);
bookRef.child(current_user_id + bookRandomName).updateChildren(booksMap)
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if (task.isSuccessful()){
SendUserToHomePageActivity();
Toast.makeText(AddBookActivity.this,"წიგნი წარმატებით დაემატა!",Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}else {
String message = task.getException().getMessage();
Toast.makeText(AddBookActivity.this,"დაფიქსირდა შეცდომა" + message,Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==Gallery_Pick && resultCode==RESULT_OK && data!=null){
ImageUrl = data.getData();
selectBookImage.setImageURI(ImageUrl);
}
}
}
这是我在运行应用程序并将书添加到数据库时获取的错误,请注意,图像已成功上传,然后导致应用程序崩溃:
2018-11-11 02:03:13.068 29281-29281/com.example.t3hjeff.bookshelf E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.t3hjeff.bookshelf, PID: 29281
com.google.firebase.database.DatabaseException: Found conflicting getters for name: getText
答案 0 :(得分:3)
请注意以下行:
bookTitle = (EditText) findViewById(R.id.addbookTitle);//bookTitle is an EditText object
...
booksMap.put("title",bookTitle);//it can not be serialized/deserialized
Firebase数据库只能存储JSON类型,因此您不能将EditText
传递给它。
Title = bookTitle.getText().toString();
...
booksMap.put("title",Title );//modify like this.