Ошибка базы данных Android Firebase Realtime при получении дочерних узлов - PullRequest
1 голос
/ 09 апреля 2019

У меня довольно странная проблема. Этот код работал несколько дней назад, но теперь он ломает мое приложение. Я пытаюсь перебрать узлы детей.

private void getInfo()
{
    FirebaseDatabase.getInstance().getReference()
            .child("ForumResponses").child(forumID).addListenerForSingleValueEvent(new ValueEventListener()
    {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {
            for(DataSnapshot snap : dataSnapshot.getChildren())
            {
                ForumResponses forumResponses = snap.getValue(ForumResponses.class);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError)
        {

        }
    });
}

Если я удаляю приведенную ниже строку кода, приложение не падает. Почему это?

ForumResponses forumResponses = snap.getValue(ForumResponses.class);

Ниже приведен журнал ошибок

    com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to int
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertInteger(com.google.firebase:firebase-database@@16.1.0:351)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(com.google.firebase:firebase-database@@16.1.0:272)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:197)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database@@16.1.0:178)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database@@16.1.0:47)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.1.0:580)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database@@16.1.0:550)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@16.1.0:420)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@16.1.0:214)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@16.1.0:79)
    at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@16.1.0:212)
    at Activities.ViewForumResponseActivity$1.onDataChange(ViewForumResponseActivity.java:95)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
    at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)

Ниже приведен класс ответа форума

public class ForumResponses
{
private String id;
private String response;
private String author;
private String authorID;
private String usersWhoLiked;

private int liked;

public ForumResponses()
{
    /*
    Default no arg constructor
     */
}

public ForumResponses(String id, String response, String author, String authorID, String usersWhoLiked, int liked)
{
    this.id = id;
    this.response = response;
    this.author = author;
    this.authorID = authorID;
    this.usersWhoLiked = usersWhoLiked;
    this.liked = liked;
}

public String getId()
{
    return id;
}

public void setId(String id)
{
    this.id = id;
}

public String getResponse()
{
    return response;
}

public void setResponse(String response)
{
    this.response = response;
}

public String getAuthor()
{
    return author;
}

public void setAuthor(String author)
{
    this.author = author;
}

public String getAuthorID()
{
    return authorID;
}

public void setAuthorID(String authorID)
{
    this.authorID = authorID;
}

public String getUsersWhoLiked()
{
    return usersWhoLiked;
}

public void setUsersWhoLiked(String usersWhoLiked)
{
    this.usersWhoLiked = usersWhoLiked;
}

public int getLiked()
{
    return liked;
}

public void setLiked(int liked)
{
    this.liked = liked;
}

}

Ниже изображение базы данных

enter image description here

1 Ответ

1 голос
/ 09 апреля 2019

Вы получаете следующую ошибку:

com.google.firebase.database.DatabaseException: не удалось преобразовать значение типа java.util.HashMap в int

Поскольку тип вашего поля liked в вашем классе ForumResponses равен int, а в вашей базе данных это поле является действительным объектом.Для решения этой проблемы либо преобразуйте поле в базе данных в int, либо измените тип поля в классе ForumResponses на Map<String, Object>.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...