'setValue' Android Studio shows error

Viewed 3074

we are trying to connect Firebase to Android Studio. The first two steps are marked with a green tick, but for some reason when we try to set the value of myRef, Android Studio says 'Cannot resolve symbol 'setValue'. 'setValue' is coloured red.

Screenshot of code:

enter image description here

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("message");

myRef.setValue("Hello, World!");
3 Answers

The statement myRef.setValue("Hello, World!"); is misplaced. It must be inside a method body. Move it inside the onCreate() or onStart() method.

You'll need to call push() to generate a unique key for the new data:

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference() 
mDatabase.push().setValue("Hello, World!");

check this for more details

Include the URL of your database in getInstance()

FirebaseDatabase database = FirebaseDatabase.getInstance("https://testfirebase-98765-default-rtdb.asia-southeast1.firebasedatabase.app/");
DatabaseReference myRef = database.getReference("message");
myRef.setValue("Hello, World!");
Related