3

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,162
  • 20
  • 85
  • 137
user8465176
  • 35
  • 1
  • 5

3 Answers3

3

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
  • 36,508
  • 5
  • 108
  • 154
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,162
  • 20
  • 85
  • 137
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!");