I am making a project in which when a sensor is tripped it sends the data to the Firebase realtime database and then to be outputted in an Android application. Firebase creates a unique key for the data once it enters the database so I am unsure how to retrieve the data.
error that appears when app crashes
I have tried some code to output the data but it just crashes the App. The app opens onto the page that is trying to display the data.
DatabaseReference databaseReference =
FirebaseDatabase.getInstance().getReference();
Query lastQuery = databaseReference.child("sensor").orderByKey().limitToLast(1);
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String message = dataSnapshot.child("Detection").getValue().toString();
sensorValue.setText(message);
}
@Override
public void onCancelled(DatabaseError databaseError) {
//Handle possible errors.
}
});