DatabaseReference.setValue(pojo)上的ServerValue.timeStamp出错

时间:2017-02-03 18:03:57

标签: android firebase firebase-realtime-database

我尝试将数据存储在firebase数据库中,但发生以下错误:

  

流程:com.example.zar.shoppinglistplusfirebase,PID:2379                     java.lang.RuntimeException:java.lang.reflect.InvocationTargetException                         在com.google.android.gms.internal.zzbqi $ zza.zzaF(未知来源)                         在com.google.android.gms.internal.zzbqi.zzax(未知来源)                         在com.google.android.gms.internal.zzbqi.zzaw(未知来源)                         在com.google.firebase.database.DatabaseReference.zza(未知来源)                         在com.google.firebase.database.DatabaseReference.setValue(未知来源)                         在com.example.zar.shoppinglistplusfirebase.ui.activeLists.AddListsFragment.addShoppingList(AddListsFragment.java:115)                         在com.example.zar.shoppinglistplusfirebase.ui.activeLists.AddListsFragment $ 1.onEditorAction(AddListsFragment.java:74)

Pojo ShoppingList

package com.example.zar.shoppinglistplusfirebase.model;

import com.example.zar.shoppinglistplusfirebase.Utils.Constants;
import com.google.firebase.database.ServerValue;
import java.util.HashMap;



public class ShoppingList {
private String listName;
private String owner;
private HashMap<String, Object> timestampLastChanged;

/**
 * Required public constructor
 */
public ShoppingList() {
}

/**
 * Use this constructor to create new ShoppingLists.
 * Takes shopping list listName and owner. Set's the last
 * changed time to what is stored in ServerValue.TIMESTAMP
 *
 * @param listName
 * @param owner
 *
 */
public ShoppingList(String listName, String owner,HashMap<String,Object> timampLastChangedObj) {
    this.listName = listName;
    this.owner = owner;
    HashMap<String, Object> timestampLastChangedObj = new HashMap<String, Object>();
    timestampLastChangedObj.put(Constants.FIREBASE_PROPERTY_TIMESTAMP, ServerValue.TIMESTAMP);
    this.timestampLastChanged = timestampLastChangedObj;

}

public String getListName() {
    return listName;
}

public String getOwner() {
    return owner;
}

public HashMap<String, Object> getTimestampLastChanged() {
    return timestampLastChanged;
}



public long getTimestampLastChangedLong() {

    return (long) timestampLastChanged.get(Constants.FIREBASE_PROPERTY_TIMESTAMP);
}
}

数据写入firebase

  FirebaseDatabase firebaseDatabase=FirebaseDatabase.getInstance();
        DatabaseReference databaseReference=firebaseDatabase.getReferenceFromUrl(Constants.FIREBASE_ACTIVE_LIST_URL);
        DatabaseReference ref=databaseReference.push();

        HashMap<String,Object> timeStampCreated=new HashMap<>();
        timeStampCreated.put(Constants.FIREBASE_PROPERTY_TIMESTAMP, ServerValue.TIMESTAMP);

        ShoppingList newShoppingList = new ShoppingList(userEnteredName, owner,timeStampCreated);
        ref.setValue(newShoppingList);
        AddListsFragment.this.getDialog().cancel();

1 个答案:

答案 0 :(得分:0)

Firebase会看到您的getTimestampLastChangedLong() getter,并查找不存在的相应属性。对@Exclude方法使用getTimestampLastChangedLong()注释,Firebase会忽略它。这里有更详细的解释:https://stackoverflow.com/a/36659507/4816918