Android DatabaseReference.setValue(pojo)处的ServerValue.timeStamp出错
我试图在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.activelist.AddListsFragment.addShoppingList(AddListsFragment.java:115)上 在com.example.zar.shoppinglistplusfirebase.ui.activeLists.AddListsFragment$1.onEditorAction(AddListsFragment.java:74)上 Pojo购物清单Android DatabaseReference.setValue(pojo)处的ServerValue.timeStamp出错,android,firebase,firebase-realtime-database,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.zz
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);
}
}
package com.example.zar.shoppinglistplusfirebase.model;
导入com.example.zar.shoppinglistplusfirebase.Utils.Constants;
导入com.google.firebase.database.ServerValue;
导入java.util.HashMap;
公共类购物清单{
私有字符串列表名;
私人字符串所有者;
私有HashMap时间戳已更改;
/**
*必需的公共构造函数
*/
公众购物清单(){
}
/**
*使用此构造函数创建新的购物列表。
*获取购物清单列表名称和所有者。设置为最后一个
*将时间更改为存储在ServerValue.TIMESTAMP中的时间
*
*@param listName
*@param-owner
*
*/
public ShoppingList(字符串列表名、字符串所有者、HashMap timampLastChangedObj){
this.listName=listName;
this.owner=所有者;
HashMap timestampLastChangedObj=新HashMap();
timestampLastChangedObj.put(Constants.FIREBASE\u PROPERTY\u TIMESTAMP,ServerValue.TIMESTAMP);
this.timestastlastchanged=timestastlastchangedobj;
}
公共字符串getListName(){
返回列表名;
}
公共字符串getOwner(){
归还所有人;
}
公共HashMap getTimestampLastChanged(){
返回时间戳已更改;
}
公共长getTimestampLastChangedLong(){
return(long)timestastchanged.get(Constants.FIREBASE\u PROPERTY\u 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();
FirebaseDatabase-FirebaseDatabase=FirebaseDatabase.getInstance();
DatabaseReference DatabaseReference=firebaseDatabase.getReferenceFromUrl(Constants.FIREBASE\u ACTIVE\u LIST\u URL);
databasereferef=DatabaseReference.push();
HashMap timeStampCreated=新建HashMap();
timeStampCreated.put(Constants.FIREBASE\u PROPERTY\u TIMESTAMP,ServerValue.TIMESTAMP);
ShoppingList newShoppingList=新的ShoppingList(userEnteredName、owner、timeStampCreated);
ref.setValue(newShoppingList);
AddListsFragment.this.getDialog().cancel();
Firebase查看您的getTimestampLastChangedLong()
getter并查找不存在的相应属性。对getTimestampLastChangedLong()
方法使用@Exclude
注释,Firebase将忽略它。这里有更详细的解释: