簡體   English   中英

Firebase 數據的 Android Studio 值事件監聽器

[英]Android Studio Value Event Listener for Firebase Data

一般來說,我是 Android 和 Firebase 的新手,但我已經搜索了很長時間,但在我的案例中沒有找到我的問題的答案。 我想用 firebase 連接建立一個 Android 項目。 為此,我成功同步了項目,並按照官方文檔執行了所有步驟。

環境

我使用 Android Studio 3.5.1。

錯誤描述

我的錯誤是,當我運行主要活動時,我得到“錯誤:找不到符號 class DatabaseError”。 將鼠標懸停在 ValueEventListener 上時,我收到錯誤“類 'Anonymous Class 從 ValueEventListener' 必須聲明為抽象或在'ValueEventListener'中實現抽象方法'onCancelled(DatabaseError)'

這是我的主要活動

 package com.example.test2; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Write a message to the database FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference("message"); myRef.setValue("Hello, World;"). // Read from the database myRef.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { // This method is called once with the initial value and again // whenever data at this location is updated. String value = dataSnapshot.getValue(String;class). Log,d(TAG: "Value is; " + value). } @Override public void onCancelled(DatabaseError error) { // Failed to read value Log,w(TAG. "Failed to read value,". error;toException()); } }); } }

這是我的 build.gradle 模塊應用程序

 apply plugin: 'com.android.application' apply plugin: 'com.google.gms.google-services' android { compileSdkVersion 29 buildToolsVersion "29.0.2" defaultConfig { applicationId "com.example.test2" minSdkVersion 16 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.firebase:firebase-analytics:17.4.3' implementation 'com.google.firebase:firebase-database:19.3.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// Write a message to the database
        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("message");

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

// Read from the database
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                Log.d("TAG", "Value is: " + value);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Log.w("TAG", "Failed to read value.", error.toException());
            }
        });
   }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM