I don't know why dataSnapshot doesn't work.
symbol: variable dataSnapshot
private void Startplaylist(String mood) {
DatabaseReference p_ref = FirebaseDatabase.getInstance().getReference("Playlist");
//DatabaseReference p_music = (DatabaseReference) p_ref.child("Playlist_Mood").equalTo(mood);
p_ref.child("Playlist_ID").equalTo(0).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
music_id = dataSnapshot.getValue("Playlist_Id"); // Problem HERE!!
str_musictitle.setText(music_id);
//music_id = snapshot.getValue(String.class);
//str_musictitle.setText(music_id);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
This is my build.gradle(Module: app) code.
dependencies {
/-- fire storage--/
implementation 'com.github.bumptech.glide:glide:4.11.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'
implementation platform('com.google.firebase:firebase-bom:28.4.1')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.google.firebase:firebase-database:19.2.1'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
I searched hard and looked for a solution, but I couldn't solve it, so I asked a question.
You are seeing that dataSnapshot
in red because there is no such object present in your code. Most likely you should use the snapshot
object that is present as an argument. So please change the following line of code:
music_id = dataSnapshot.getValue("Playlist_Id");
To:
music_id = snapshot.getValue("Playlist_Id");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.