【问题标题】:(Android & Firebase) Cannot resolve symbol 'dataSnapshot' [closed](Android和Firebase)无法解析符号“dataSnapshot”[关闭]
【发布时间】:2021-12-31 00:59:25
【问题描述】:

我不知道为什么 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) {

            }
        });
    }


这是我的 build.gradle(Module: app) 代码。

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'
}

我苦苦搜索,寻找解决方案,但我无法解决,所以我问了一个问题。

【问题讨论】:

  • 我投票结束这个问题,因为请不要使用代码截图,使用 WYSIWYG 格式化它
  • 不应该只是快照吗?
  • 只是一个简单的错字。 var 定义为 snapshot 此处为 (@NonNull DataSnapshot snapshot) ,之后您将使用 snapShot 此处为 music_id = dataSnapshot 。由于打字错误而关闭

标签: java android firebase firebase-realtime-database google-cloud-platform


【解决方案1】:

您看到dataSnapshot 是红色的,因为您的代码中不存在这样的对象。您很可能应该使用作为参数存在的snapshot 对象。所以请更改以下代码行:

music_id = dataSnapshot.getValue("Playlist_Id");

收件人:

music_id = snapshot.getValue("Playlist_Id");

【讨论】:

    猜你喜欢
    • 2016-10-06
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2017-08-09
    • 2015-03-11
    • 1970-01-01
    相关资源
    最近更新 更多