我的应用程序运行正常,但是突然出现错误:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.NoteModelActivity
我正在使用recyclerView。 而且我无法理解在正常工作的应用中突然从何处出现此问题。 一旦我打开活动,应用程序就会崩溃。而在没有任何更改的情况下,它运行得很好。
我阅读了此问题的解决方案,但答案与Value侦听器中的“ toString”有关,但我已经实现了。.
请帮助我。
我的日志猫:
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.NoteModelActivity
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:435)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:231)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:79)
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:29)
at com.firebase.ui.database.ClassSnapshotParser.parseSnapshot(ClassSnapshotParser.java:15)
at com.firebase.ui.common.BaseCachingSnapshotParser.parseSnapshot(BaseCachingSnapshotParser.java:36)
at com.firebase.ui.common.BaseObservableSnapshotArray.get(BaseObservableSnapshotArray.java:52)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:109)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:149)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7065)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7107)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6012)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6279)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:561)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:170)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897)
at androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1147)
at android.view.Choreographer.doCallbacks(Choreographer.java:958)
at android.view.Choreographer.doFrame(Choreographer.java:881)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1133)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:226)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:500)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:913)
我的模型班:
public class NoteModelActivity {
public String noteTitle;
public String noteTime;
public NoteModelActivity() {
}
public NoteModelActivity(String noteTitle, String noteTime) {
this.noteTitle = noteTitle;
this.noteTime = noteTime;
}
public String getNoteTitle() {
return noteTitle;
}
public void setNoteTitle(String noteTitle) {
this.noteTitle = noteTitle;
}
public String getNoteTime() {
return noteTime;
}
public void setNoteTime(String noteTime) {
this.noteTime = noteTime;
}
我的活动:
fAuth = FirebaseAuth.getInstance();
if (fAuth.getCurrentUser() != null) {
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child(fAuth.getCurrentUser().getUid());
}
loadData();
}
private void loadData() {
FirebaseRecyclerOptions<NoteModelActivity> options =
new FirebaseRecyclerOptions.Builder<NoteModelActivity>()
.setQuery(fNotesDatabase , NoteModelActivity.class)
.build();
FirebaseRecyclerAdapter<NoteModelActivity, NoteViewHolderActivity>
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<NoteModelActivity, NoteViewHolderActivity>(options) {
@NonNull
@Override
public NoteViewHolderActivity onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_note_layout, parent, false);
NoteViewHolderActivity viewHolder = new NoteViewHolderActivity(view);
return viewHolder;
}
@Override
protected void onBindViewHolder(@NonNull final NoteViewHolderActivity holder, int position, @NonNull NoteModelActivity model) {
final String noteId = getRef(position).getKey();
fNotesDatabase.child(noteId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("title") && dataSnapshot.hasChild("timestamp")) {
String title = dataSnapshot.child("title").getValue(String.class);
String timestamp = dataSnapshot.child("timestamp").getValue(String.class);
holder.setNoteTitle(title);
GetTimeAgo getTimeAgo = new GetTimeAgo();
holder.setNoteTime(getTimeAgo.getTimeAgo(Long.parseLong(timestamp), getApplicationContext()));
holder.noteCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(PostActivity.this, NewNoteActivity.class);
intent.putExtra("noteId", noteId);
startActivity(intent);
}
});
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
mNotesList.setAdapter(firebaseRecyclerAdapter);
firebaseRecyclerAdapter.startListening();
}
现在,它显示了另一个活动的另一个错误,这是另一个recyclerview活动的模型,这两个arent甚至相关,而上述活动是崩溃的。.
Class com..postsRecyclerView does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.
另一个模型类;
public class postsRecyclerView {
public postsRecyclerView(String username, String article, String title, String date, String timestamp) {
this.username = username;
this.article = article;
this.title = title;
this.date = date;
this.timestamp = timestamp;
}
public String username, title,article, date, timestamp;
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getArticle() {
return article;
}
public void setArticle(String article) {
this.article = article;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
我的Firebase数据库:
非常感谢您的帮助... 请帮助我...
答案 0 :(得分:0)
为您的postRecyclerview类添加一个不带参数的构造函数,这将解决 “未定义无参数构造函数”。单击ctrl + alt + insert,然后单击构造函数,然后单击不选择。对于第一个错误,您似乎在数据库中有一个字符串,并且检索方法无效。您可以上传数据库的图片,以便我解决您的问题。