本文介绍了com.google.firebase.database.DatabaseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我开始活动时,它显示错误:

When I start the activity, it is showing the error:

protected void onStart() {
        super.onStart();
        ref.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //artistList.clear();
                for(DataSnapshot artistSnapshot : dataSnapshot.getChildren()){
                    Artist artist = artistSnapshot.getValue(Artist.class);
                    Toast.makeText(MainActivity.this, artist.getArtistName(), Toast.LENGTH_SHORT).show();

                }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

Artist.java

Artist.java

package com.rcpl.agni;

/**
 * Created by Dell-pc on 23-01-2018.
 */

public class Artist {
    String artistId;
    String artistName;
    String artistGenre;

    public Artist(String artistId, String artistName, String artistGenre) {
        this.artistId = artistId;
        this.artistName = artistName;
        this.artistGenre = artistGenre;
    }

    public String getArtistId() {
        return artistId;
    }

    public String getArtistName() {
        return artistName;
    }

    public String getArtistGenre() {
        return artistGenre;
    }
}

推荐答案

要解决此问题,您只需将public no-argument constructor添加到您的Artist类中.

To solve this, you just need to add the public no-argument constructor to your Artist class.

public Artist() {}

请记住,当Firebase Realtime Database SDK对来自数据库的对象进行反序列化时,它要求任何对象都必须具有公共的无参数构造函数.实例化该特定对象是必需的.通过使用公共设置方法或直接访问公共成员来设置对象内的所有字段.

Remember, when the Firebase Realtime Database SDK deserializes objects coming from the database, it requires that any objects to a have a public no-argument constructor. This is needed to instantiate that particular object. All the fields within the object are set by using public setter methods or direct access to public members.

如果您不使用公共的无参数构造函数,则SDK不知道如何创建该类的实例.大多数序列化库将具有相同的要求.

If you don't use a public no-arg constructor, the SDK doesn't know how to create an instance of the class. Most serialization libraries will have the same requirement.

这篇关于com.google.firebase.database.DatabaseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 08:00