Firebase数据库不适用于Android模拟器

时间:2020-08-02 12:31:47

标签: android firebase firebase-realtime-database

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

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

Gradle

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
}

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'

}

编辑1: 添加了代码(它可以在默认仿真器像素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) {

            }
        });
}

1 个答案:

答案 0 :(得分:2)

您需要自行进行更多的故障排除,以找出问题所在。我将采取的一些步骤:

  1. 停止忽略可能的错误,并实施onCancelled。最低应为public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }
  2. 确保可以在第二个模拟器上使用Internet。那么其他应用程序还能用吗?
  3. Enabling debug logging并检查logcat输出中的情况。