Mohamed Khaled
Mohamed Khaled

Reputation: 13

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

Iam Facing Error : [ Expected a Map while deserializing, but got a class java.lang.Long ] For Long Time .. I Tried to solved but not work

That Is 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();
    }


}

And My Note Class Is :

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;
    }

And My Error Is .:.

Process: com.example.notewithfirebase, PID: 4831 com.google.firebase.database.DatabaseException: Expected a Map while deserializing, but got a class java.lang.Long at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.expectMap(com.google.firebase:firebase-database@@19.2.1:344) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToParameterizedType(com.google.firebase:firebase-database@@19.2.1:261) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@19.2.1:176) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@19.2.1:47) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.1:592) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@19.2.1:562) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.2.1:432) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.2.1:231) at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.2.1:79) at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.2.1:203) at com.example.notewithfirebase.MainActivity$2.onDataChange(MainActivity.java:67) at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.2.1:75) at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.2.1:63) at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.2.1:55) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7356) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Upvotes: 1

Views: 354

Answers (1)

Blundell
Blundell

Reputation: 76496

Your Error:

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

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

The code for that error:

    @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);
    }

The likely problem:

 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

Try doing this:

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

or alternatively, put some logs in to see what you are getting:

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

Upvotes: 1

Related Questions