gpt4 book ai didi

java - com.google.firebase.database.DatabaseException : Expected a Map while deserializing, 但得到了一个类 java.lang.Long

转载 作者:行者123 更新时间:2023-11-30 05:18:01 30 4
gpt4 key购买 nike

我面临错误:[反序列化时需要一个映射,但得到一个 java.lang.Long 类]很长一段时间..我尝试解决但不起作用

那就是MainActivity.java

public class MainActivity extends AppCompatActivity{

Button add_Dialoge;
FloatingActionButton add;
EditText title_Dialoge , note_Dialoge;
ListView listViewNote;
String title , note , mId;

FirebaseDatabase mDataBase = FirebaseDatabase.getInstance();
DatabaseReference mRef = mDataBase.getReference("Notes");

ArrayList<Note> mNoteList = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

add = findViewById(R.id.BTN_Add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAlertDialogeAddNote(); }
});

listViewNote = findViewById(R.id.LV_ListViewNote);

mNoteList = new ArrayList<>();

}

@Override
protected void onStart() {
super.onStart();

mRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot data) {
// That Run When I Want To Get Data From FireBase

for (DataSnapshot snapshot : data.getChildren()){
Note note = snapshot.getValue(Note.class);
mNoteList.add(note);
}
noteAdapter adapter = new noteAdapter(MainActivity.this , mNoteList);
listViewNote.setAdapter(adapter);
}

@Override
public void onCancelled(@NonNull DatabaseError error) {
// That Run When Have Error
Log.e("The Read Failed Is .:. " ,error.getMessage());
}

});

}

void showAlertDialogeAddNote() {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.layout_add_note , null);

title_Dialoge = view.findViewById(R.id.ET_Title_DialogAdd);
note_Dialoge = view.findViewById(R.id.ET_Note_DialogAdd);

add_Dialoge = view.findViewById(R.id.BTN_Add_DialogAdd);
add_Dialoge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

title = title_Dialoge.getText().toString();
note = note_Dialoge.getText().toString();

if (title.isEmpty() && note.isEmpty()) {
Toast.makeText(MainActivity.this, "!! ERROR .. The Title Or Note Or Both Are Empty !!", Toast.LENGTH_LONG).show();
} else {
mId = mRef.push().getKey();
Note myNote = new Note(mId, title , note , ServerValue.TIMESTAMP);
mRef.child(mId).setValue(myNote);
}
}
});

alertBuilder.setView(view);
AlertDialog alertDialog = alertBuilder.create();
alertDialog.show();
}


}

我的笔记类别是:

public class Note {

public String id;
public String title , note;
public Map<String, String> time;

public Note() {
}

Note(String id, String title, String note, Map<String, String> time) {
this.id = id;
this.title = title;
this.note = note;
this.time = time;
}

我的错误是.:.

进程:com.example.notewithfirebase,PID:4831 com.google.firebase.database.DatabaseException:反序列化时需要一个 Map,但得到一个类 java.lang.Long 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.expectMap(com.google.firebase:firebase-database@@19.2.1:344) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToParameterizedType(com.google.firebase:firebase-database@@19.2.1:261) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@19.2.1:176) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@19.2.1:47) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.1:592) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.1:562) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.2.1:432) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:231) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.1:79) 在 com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.1:203) 在 com.example.notewithfirebase.MainActivity$2.onDataChange(MainActivity.java:67) 在 com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75) 在 com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63) 在 com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55) 在 android.os.Handler.handleCallback(Handler.java:883) 在 android.os.Handler.dispatchMessage(Handler.java:100) 在 android.os.Looper.loop(Looper.java:214) 在 android.app.ActivityThread.main(ActivityThread.java:7356) 在 java.lang.reflect.Method.invoke( native 方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

最佳答案

您的错误:

firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.lang.Long

MainActivity$2.onDataChange(MainActivity.java:67) at

该错误的代码:

    @Override
public void onDataChange(@NonNull DataSnapshot data) {
// That Run When I Want To Get Data From FireBase

for (DataSnapshot snapshot : data.getChildren()){
Note note = snapshot.getValue(Note.class);
mNoteList.add(note);
}
noteAdapter adapter = new noteAdapter(MainActivity.this , mNoteList);
listViewNote.setAdapter(adapter);
}

可能的问题:

 Note note = snapshot.getValue(Note.class);

Note(String id, String title, String note, Map<String, String> time) {

Expected a Map while deserializing, but got a class java.lang.Long

尝试这样做:

Note(String id, String title, String note, Long time) {

或者,输入一些日志来查看您得到了什么:

 for (DataSnapshot snapshot : data.getChildren()){
Log.d("TUT", "Got snapshop " + snapshot);
Note note = snapshot.getValue(Note.class);
mNoteList.add(note);
}

关于java - com.google.firebase.database.DatabaseException : Expected a Map while deserializing, 但得到了一个类 java.lang.Long,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60066933/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com