确保首先调用FirebaseApp.initializeApp(Context)。错误未将消息发送到Firebase且应用程序已终止

时间:2019-06-03 17:56:58

标签: android kotlin

我只有三行代码将msg发送到firebase,当我运行应用程序时,它会自动终止。我通过“工具”菜单添加了Firebase。

我尝试了Internet权限来显示文件,但是没有用。

var firebaseDatabase = FirebaseDatabase.getInstance()
        var databaseRef = firebaseDatabase.reference
        databaseRef.setValue("Hello There")
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.core:core-ktx:1.2.0-alpha01'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
implementation 'com.google.android.material:material:1.0.0-beta01'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.1.0'
  }
}

“ I / FirebaseInitProvider:FirebaseApp初始化失败” “无法启动活动ComponentInfo {.MainActivity}:java.lang.IllegalStateException:在此过程中未初始化默认FirebaseApp。请确保首先调用FirebaseApp.initializeApp(Context)。”

1 个答案:

答案 0 :(得分:0)

如错误所述,您需要在调用FirebaseApp实例的方法之前将其初始化。这行:

FirebaseApp.initializeApp(this); // "this" is a context

一种常见的实现方法-将此行添加到自定义Application类的onCreate()方法中。

here, on Codepath和您可以找到如何创建自定义Application类以将该行放入其中。

更新

这也将在您在Firebase控制台中配置Firebase App项目后起作用。不要忘记执行步骤from here, Firebase docs