Firebase 数据库无法在 Android 模拟器上运行

yea*_*n14 5 android firebase firebase-realtime-database

我的应用程序正在检查 Firebase 实时数据库中是否存在某个数据,并将 addListenerForSingleValueEvent 添加到数据库引用中。我启动项目的模拟器工作正常并且完美地检索数据,但是当我更改模拟器(假设我切换到 PIXEL 3XL)时,侦听器无法工作。我在另一个 stackoverflow 问题中看到有人遇到了同样的问题,并且数据库检索了数据,但是经过很长时间。有谁知道为什么会发生这种情况?我应该继续使用默认模拟器进行开发而不关心实时数据库无法在另一个模拟器上运行吗?你能解释一下为什么会发生这种情况吗?

我在清单中使用它: ...<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>...

摇篮

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.0.1"
        classpath 'com.google.gms:google-services:4.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

gradle(模块)

apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 30
    buildToolsVersion "30.0.1"

    defaultConfig {
        applicationId "..."
        minSdkVersion 28
        targetSdkVersion 30
        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-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'

}
Run Code Online (Sandbox Code Playgroud)

编辑1:添加代码(它适用于默认模拟器,pixel 3,但不适用于其他模拟器)日志只是为了查看代码是否正在执行其应该执行的操作。

DatabaseReference database, newRef;

 protected void onCreate(Bundle savedInstanceState) {
(...)
database = FirebaseDatabase.getInstance().getReference();
newRef = database.child(option).child(strSelectedYear).child(strSelectedMonth).child(strSelectedDay);
        newRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot d: dataSnapshot.getChildren()) {
                    hours.remove(d.getKey());
                    Log.i("hAI FRAAA", d.getKey());

                }
                String msg = "";
                for (int j = 0;j<hours.size();j+=1)
                    msg += " " + hours.get(j);
                Log.i("Free hours", msg);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
}
Run Code Online (Sandbox Code Playgroud)

Fra*_*len 2

您需要自己进行更多故障排除才能找出问题所在。我会采取的几个步骤:

  1. 停止忽略可能的错误并实施onCancelled。至少应该是public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }
  2. 确保第二个模拟器上的互联网访问正常。那么其他应用程序也能工作吗?
  3. 启用调试日志记录并在 logcat 输出中检查发生了什么。