【问题标题】:com.google.firebase.database.DatabaseException : Expected a Map while deserializing, but got a class java.lang.Longcom.google.firebase.database.DatabaseException :反序列化时需要一个 Map,但得到一个类 java.lang.Long
【发布时间】:2020-05-20 20:16:06
【问题描述】:

我面临错误:[在反序列化时需要一个 Map,但得到了一个类 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(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

【问题讨论】:

    标签: java android firebase


    【解决方案1】:

    你的错误:

    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) {
    

    在反序列化时需要一个 Map,但得到一个类 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);
            }
    

    【讨论】:

      猜你喜欢
      • 2016-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-17
      • 2020-04-04
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多