com.google.firebase.database.DatabaseException: ожидал карту при десериализации, но получил класс java .lang.Long - PullRequest
1 голос
/ 05 февраля 2020

Iam Facing Error: [Ожидал карту при десериализации, но получил класс 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: ожидал карту при десериализации, но получил класс 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 ( .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. липкая gle.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 @@ 19.2.1: 55) в android .os.Handler.handleCallback (Обработчик. java: 883) в android .os.Handler.dispatchMessage (Обработчик. java: 100) в android .os.Looper.l oop ( 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)

1 Ответ

1 голос
/ 05 февраля 2020

Ваша ошибка:

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

Ожидается карта во время десериализацию, но получил класс 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);
        }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...