Firebase数据库InvocationTargetException

时间:2016-08-02 00:24:16

标签: android database firebase firebase-realtime-database

当我尝试将对象写入Firebase数据库时,我收到此错误,错误如下:

`

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
                                                                           at com.google.android.gms.internal.zzaix$zza.zzaE(Unknown Source)
                                                                           at com.google.android.gms.internal.zzaix.zzaw(Unknown Source)
                                                                           at com.google.android.gms.internal.zzaix.zzav(Unknown Source)
                                                                           at com.google.firebase.database.DatabaseReference.zza(Unknown Source)
                                                                           at com.google.firebase.database.DatabaseReference.setValue(Unknown Source)

当我尝试从Firebase添加ServerValue.TIMESTAMP时出现错误,由Udacity中的示例引导,这是我的POJO:

public class Messages {

    private String content, author, authorPhoto;
    private int type;
    protected HashMap<String, Object> timeStampCreated;
    private Uri uri;


    public Messages() {//Empty default constructor, necessary for FireBase to be able to deserialize blog posts
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getContent() {
        return content;
    }

    public String getAuthor() {
        return author;
    }

    public String getAuthorPhoto() {
        return authorPhoto;
    }

    public int getType() {
        return type;
    }

    @JsonIgnore
    public long getTimeStamp() {
        return (long)timeStampCreated.get("timeStamp");
    }

    public void setTimeStamp() {
        HashMap<String, Object> hmDate = new HashMap<>();
        hmDate.put("timeStamp", ServerValue.TIMESTAMP);
        this.timeStampCreated = hmDate;
    }

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

    public void setAuthorPhoto(String authorPhoto) {
        this.authorPhoto = authorPhoto;
    }

    public void setType(int type) {
        this.type = type;
    }

    public Uri getUri() {
        return uri;
    }

    public void setUri(Uri uri) {
        this.uri = uri;
    }
}

这是我尝试将此Messages对象添加到Firebase数据库的方法,确切的错误是我设置值时:

public void sendMessage(String content, final int type) {
        Messages message = new Messages();
        message.setContent(content);
        message.setTimeStamp();
        message.setAuthor(email);
        message.setAuthorPhoto(picURL);
        message.setType(type);

        FBRefMessages.push().setValue(message, new DatabaseReference.CompletionListener() {
            @Override
            public void onComplete(DatabaseError error, DatabaseReference databaseReference) {
                if (error == null) {
                    Log.d("Adapter", "onChildAdded: The adapter is: " + mAdapter);
                    updateNodes();
                } else {//There was an error
                }
            }
        });
    }

请帮忙!

2 个答案:

答案 0 :(得分:2)

Uri不是受支持的数据类型,请参阅https://www.firebase.com/docs/android/guide/understanding-data.html

尝试用String替换它。

答案 1 :(得分:0)

我对此进行了一段时间的实验,并得出结论认为不可能按照您的意愿在POJO中使用ServerValue.TIMESTAMP

如您所知,ServerValue.TIMESTAMPMap<String,String>。在非POJO用例中,您可以将其作为Object参数提供给setValue(),或者作为Map<String,Object>的元素提供给setValue()。处理数据存储请求时,Firebase服务器会识别特殊标记值并将其替换为服务器时间。随后读取数据时,它将作为Long返回。

用于序列化的POJO处理基于具有固定类型的字段。我不知道如何为输出中需要Map<String,String>和输入Long的字段工作。

如果您可以容忍不太准确的时间戳,您可以考虑添加一个监听器来监控时钟偏差并从中导出您自己的时间戳。这在documentation

中有所描述