4

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!");
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
M Y
  • 101
  • 2
  • 8

3 Answers3

4

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

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
2

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

Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

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!");