为什么从fire base检索数据时它检索null?

时间:2017-02-01 12:15:40

标签: android firebase firebase-realtime-database

当我尝试从firebase检索数据时,它检索到一个空对象,并且数据已经存在于子节点中这个firebase的状态

The firebase tree

sensorValue
{
  "ldrSensorValue" : 1021,
  "potSensorValue" : 820,
  "temperatureSensorValue" : 29.33
}

这是班级:

public class SensorsValues {

private double temperatureSensorValue;
private double ldrSensorValue;
private double potSensorValue;


public SensorsValues() {
}

public SensorsValues(double temperatureSensorValue, double ldrSensorValue, double potSensorValue) {
    this.temperatureSensorValue = temperatureSensorValue;
    this.ldrSensorValue = ldrSensorValue;
    this.potSensorValue = potSensorValue;
}

public double getTemperatureSensorValue() {
    return temperatureSensorValue;
}

public double getLdrSensorValue() {
    return ldrSensorValue;
}

public double getPotSensorValue() {
    return potSensorValue;
}

public void setTemperatureSensorValue(double temperatureSensorValue) {
    this.temperatureSensorValue = temperatureSensorValue;
}

public void setLdrSensorValue(double ldrSensorValue) {
    this.ldrSensorValue = ldrSensorValue;
}

public void setPotSensorValue(double potSensorValue) {
    this.potSensorValue = potSensorValue;
}
}

这是我检索数据的代码的位置

    //set up the firebase
  mFirebaseDatabase = FirebaseDatabase.getInstance();
  mDatabaseReference = mFirebaseDatabase.getReference().child("sensorValue");
  mSendDatabaseReference = mFirebaseDatabase.getReference().child("sendCommands");

  mSensorsValueschildEventListener = new ChildEventListener() {
        //when new message is inserted this method is called
        //for every child is exist this will called
        //DataSnapshot contain data from the database at certain position
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {


        }

        //this called when the content of exiting data is changed
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {
            //pass class when the child has the same child in the class
           SensorsValues sensorsValues =  dataSnapshot.child("sensorValue").getValue(SensorsValues.class);


            //get the sensors value
           /* tempSensorValue = sensorsValues.getTemperatureSensorValue();
            ldrSensorValue = sensorsValues.getLdrSensorValue();
            potSensorValue = sensorsValues.getPotSensorValue();*/
            Log.e(LOG_TAG,sensorsValues+"");

        }
        //this called when the content of exiting data is removed

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }
        //this called when the content of exiting data is moved mean change it's position

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        //call when error happen when try to change some data
        //most when you don't have permission to read the data
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    };
    mDatabaseReference.addChildEventListener(mSensorsValueschildEventListener);

2 个答案:

答案 0 :(得分:1)

我认为错误在于Firebase认为类型为Long的一个(或多个)元素(基于您将它们存储到Firebase数据库中的方式)无法将其转换为double(内部变量的类型) SensorValues类)。

因此,要解决错误,请尝试存储(例如)734.0而不是734(依此类推)。通过这种方式,Firebase可以正确地将数字转换为加倍。

希望你能解决。

答案 1 :(得分:0)

我认为您不需要在数据快照上调用.child(“sensorValue”) 这样做

 SensorsValues sensorsValues =  dataSnapshot.getValue(SensorsValues.class);