com.google.firebase.database.DatabaseException:类com.chatt.demo.model.Conversation缺少没有参数的构造函数

时间:2017-01-11 18:16:36

标签: android firebase firebase-realtime-database

public class Chat extends CustomActivity {
     //some coding and functions
    FirebaseDatabase.getInstance().getReference("messages").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
            if(user != null) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    Conversation conversation = ds.getValue(Conversation.class);
                    if (conversation.getReceiver().contentEquals(user.getUid()) || //here is <<error>> conversation.getSender().contentEquals(user.getUid())) {
                        convList.add(conversation);
                        if (lastMsgDate == null
                                || lastMsgDate.before(conversation.getDate()))
                            lastMsgDate = conversation.getDate();

                        adp.notifyDataSetChanged();
                    }
                }
            }
        }
    }
}

这是我的conversation班级

public class Conversation{

    /** The Constant STATUS_SENDING. */
    public static final int STATUS_SENDING = 0;

    /** The Constant STATUS_SENT. */
    public static final int STATUS_SENT = 1;

    /** The Constant STATUS_FAILED. */
    public static final int STATUS_FAILED = 2;

    /** The msg. */
    private String msg;

    /** The status. */
    private int status = STATUS_SENT;

    /** The date. */
    private Date date;

    /** The sender. */
    private String sender;

    /** The receiver */
    private String receiver;

    /** The photo url. */
    private String photoUrl;

    /**
     * Instantiates a new conversation.
     * 
     * @param msg
     *            the msg
     * @param date
     *            the date
     * @param sender
     *            the sender
     * @param receiver
     *            the receiver
     * @param photoUrl
     *            the photo url
     */
    public Conversation(String msg, Date date, String sender, String receiver, String photoUrl) {
        this.msg = msg;
        this.date = date;
        this.sender = sender;
        this.receiver = receiver;
        this.photoUrl = photoUrl;
    }

    /**
     * Gets the msg.
     * 
     * @return the msg
     */
    public String getMsg()
    {
        return msg;
    }

    /**
     * Sets the msg.
     *
     * @param msg
     *            the new msg
     */
    public void setMsg(String msg)
    {
        this.msg = msg;
    }

    /**
     * Checks if is sent.
     * 
     * @return true, if is sent
     */
    public boolean isSent()
    {
        return UserList.user.getId().contentEquals(sender);
    }

    /**
     * Gets the date.
     * 
     * @return the date
     */
    public Date getDate() {

        return date;
    }

    /**
     * Sets the date.
     *
     * @param date
     *            the new date
     */
    public void setDate(Date date)
    {
        this.date = date;
    }

    /**
     * Gets the sender.
     *
     * @return the sender
     */
    public String getReceiver()
    {
        return receiver;
    }

    /**
     * Sets the sender.
     *
     * @param receiver
     *            the new sender
     */
    public void setReceiver(String receiver)
    {
        this.receiver = receiver;
    }

    /**
     * Gets the sender.
     * 
     * @return the sender
     */
    public String getSender()
    {
        return sender;
    }

    /**
     * Sets the sender.
     *
     * @param sender
     *            the new sender
     */
    public void setSender(String sender)
    {
        this.sender = sender;
    }

    /**
     * Gets the status.
     * 
     * @return the status
     */
    public int getStatus()
    {
        return status;
    }

    /**
     * Sets the status.
     * 
     * @param status
     *            the new status
     */
    public void setStatus(int status)
    {
        this.status = status;
    }

    /**
     * Set the photo URL
     *
     * @param photoUrl
     */
    public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; }

    /**
     * Get the photo URL
     *
     * @return photoUrl
     */
    public String getPhotoUrl() { return this.photoUrl; }

}

我的Firebase数据库快照就在这里:

Firebase database format

以下是例外日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.chatt.demo, PID: 11580
              com.google.firebase.database.DatabaseException: Class com.chatt.demo.model.Conversation is missing a constructor with no arguments

先谢谢。

2 个答案:

答案 0 :(得分:1)

让您的Conversation课程如下所示。

public class Conversation{
    public static final int STATUS_SENDING = 0;
    public static final int STATUS_SENT = 1;
    public static final int STATUS_FAILED = 2;
    public String msg;
    public int status = STATUS_SENT;
    public Date date;
    public String sender;
    public String receiver;
    public String photoUrl;
}

将所有变量声明为public并像这样访问它们。

Conversation conversation = ds.getValue(Conversation.class);

if(conversation.receiver.equals(toSomething)) { 
    // Do something
}

答案 1 :(得分:1)

在Conversation.java中添加一个空构造函数

public class Conversation{

/** The Constant STATUS_SENDING. */
public static final int STATUS_SENDING = 0;

/** The Constant STATUS_SENT. */
public static final int STATUS_SENT = 1;

/** The Constant STATUS_FAILED. */
public static final int STATUS_FAILED = 2;

/** The msg. */
private String msg;

/** The status. */
private int status = STATUS_SENT;

/** The date. */
private Date date;

/** The sender. */
private String sender;

/** The receiver */
private String receiver;

/** The photo url. */
private String photoUrl;

`/** Empty constructor for Firebase*/
public Conversation(){
}

/**
 * Instantiates a new conversation.
 * 
 * @param msg
 *            the msg
 * @param date
 *            the date
 * @param sender
 *            the sender
 * @param receiver
 *            the receiver
 * @param photoUrl
 *            the photo url
 */
public Conversation(String msg, Date date, String sender, String receiver, String photoUrl) {
    this.msg = msg;
    this.date = date;
    this.sender = sender;
    this.receiver = receiver;
    this.photoUrl = photoUrl;
}

/**
 * Gets the msg.
 * 
 * @return the msg
 */
public String getMsg()
{
    return msg;
}

/**
 * Sets the msg.
 *
 * @param msg
 *            the new msg
 */
public void setMsg(String msg)
{
    this.msg = msg;
}

/**
 * Checks if is sent.
 * 
 * @return true, if is sent
 */
public boolean isSent()
{
    return UserList.user.getId().contentEquals(sender);
}

/**
 * Gets the date.
 * 
 * @return the date
 */
public Date getDate() {

    return date;
}

/**
 * Sets the date.
 *
 * @param date
 *            the new date
 */
public void setDate(Date date)
{
    this.date = date;
}

/**
 * Gets the sender.
 *
 * @return the sender
 */
public String getReceiver()
{
    return receiver;
}

/**
 * Sets the sender.
 *
 * @param receiver
 *            the new sender
 */
public void setReceiver(String receiver)
{
    this.receiver = receiver;
}

/**
 * Gets the sender.
 * 
 * @return the sender
 */
public String getSender()
{
    return sender;
}

/**
 * Sets the sender.
 *
 * @param sender
 *            the new sender
 */
public void setSender(String sender)
{
    this.sender = sender;
}

/**
 * Gets the status.
 * 
 * @return the status
 */
public int getStatus()
{
    return status;
}

/**
 * Sets the status.
 * 
 * @param status
 *            the new status
 */
public void setStatus(int status)
{
    this.status = status;
}

/**
 * Set the photo URL
 *
 * @param photoUrl
 */
public void setPhotoUrl(String photoUrl) { this.photoUrl = photoUrl; }

/**
 * Get the photo URL
 *
 * @return photoUrl
 */
public String getPhotoUrl() { return this.photoUrl; }

}

我希望它会对你有所帮助。