Android Firebase:无效区分大小写的名称

时间:2018-03-25 09:53:54

标签: java android firebase firebase-realtime-database

我正在尝试将一个Track类型对象添加到firebase数据库中,该数据库具有一个Checkpoint类型对象的ArrayList。

ssDB.child("tracks").child(trackKey).setValue(newTrack)

但是我收到了一个错误:

03-25 09:35:38.293 9189-9189/com.illbirdstudios.illbird.streetsend E/AndroidRuntime: FATAL EXCEPTION: main
 Process: com.illbirdstudios.illbird.streetsend, PID: 9189
 com.google.firebase.database.DatabaseException: Found setter with invalid case-sensitive name: setCheckpoints
     at com.google.android.gms.internal.zzelx.<init>(Unknown Source:403)
     at com.google.android.gms.internal.zzelw.zzf(Unknown Source:12)
     at com.google.android.gms.internal.zzelw.zzb(Unknown Source:756)
     at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
     at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
     at com.illbirdstudios.illbird.streetsend.CreateTrackActivity2$1$1.onDataChange(CreateTrackActivity2.java:174)
     at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)
     at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
     at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
     at com.google.android.gms.internal.zzeig.run(Unknown Source:65)
     at android.os.Handler.handleCallback(Handler.java:789)
     at android.os.Handler.dispatchMessage(Handler.java:98)
     at android.os.Looper.loop(Looper.java:164)
     at android.app.ActivityThread.main(ActivityThread.java:6541)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)

这是我的Track Class

public class Track {

    private String trackOwner;
    private String trackName;

    public final static String POINTTOPOINT = "POINTTOPOINT";
    public final static String CIRCUIT = "CIRCUIT";

    private String trackType;
    private ArrayList<Checkpoint> checkpoints;

    public Track(String trackOwner, String trackName, String trackType){

        this.trackOwner = trackOwner;
        this.trackName = trackName;
        this.trackType = trackType;
        checkpoints = new ArrayList<Checkpoint>();

    }

    public Track(){

    }

    public String getTrackOwner(){
        return this.trackOwner;
    }

    public String getTrackName(){
        return this.trackName;
    }

    public String getTrackType(){
        return this.trackType;
    }

    public void setCheckpoints(ArrayList<Checkpoint> checkpoints){
        this.checkpoints = checkpoints;
    }

    public void addCheckPoint(Checkpoint checkpoint){
        checkpoints.add(checkpoint);
    }

    public ArrayList<Checkpoint> getCheckPoints(){
        return this.checkpoints;
    }
}

当我尝试通过对数据库的子调用分别添加每个Track项时,它可以工作:

Log.d("Saving Track", "Track Name");
ssDB.child("tracks").child(trackKey).child("trackName").setValue(newTrack.getTrackName());
Log.d("Saving Track", "Track Owner");
ssDB.child("tracks").child(trackKey).child("trackOwner").setValue(newTrack.getTrackOwner());
Log.d("Saving Track", "Track Type");
ssDB.child("tracks").child(trackKey).child("trackType").setValue(newTrack.getTrackType());
Log.d("Saving Track", "Track Checkpoints");
ssDB.child("tracks").child(trackKey).child("checkpoints").setValue(newTrack.getCheckPoints());

但是当我将值设置为对象本身时不是。

ssDB.child("tracks").child(trackKey).setValue(newTrack)

2 个答案:

答案 0 :(得分:3)

由于以下字段,您收到此错误:

private ArrayList<Checkpoint> checkpoints;

正确的getter和setter应该是:

public ArrayList<Checkpoint> getCheckpoints() {
    return checkpoints;
}

public void setCheckpoints(ArrayList<Checkpoint> checkpoints) {
    this.checkpoints = checkpoints;
}

您的getter应该是getCheckpoints()而不是getCheckPoints(),因为它在您的代码中。请参见小写p与大写字母P

答案 1 :(得分:1)

您的安装人员和访客应遵循Java Bean的原则。目前你的getter和setter不匹配。

此外,请考虑不使用ArrayList存储数据列表,而是使用Maps。在Firebase文档here中,您可以找到一些存储列表的最佳做法。