我无法初始化firebase

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我无法初始化firebase相关的知识,希望对你有一定的参考价值。

我需要连接到firebase,我完全按照firebase文档告诉我为什么我必须连接到firebase。我也放了apply插件,我看到了firebase控制台,我做了我需要做的每一步。我的代码......

    FirebaseApp.initializeApp(this);

    FirebaseDatabase database = FirebaseDatabase.getInstance();

    DatabaseReference myRef= database.getReference();

    myRef.setValue("teste");

那是我的build.code(模块)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "projeto.ufma.com.projeto"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-core:16.0.4'
    implementation 'com.google.firebase:firebase-database:16.0.5'


}

但总是我收到了这个错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: projeto.ufma.com.projeto, PID: 28861
    java.lang.RuntimeException: Unable to start activity ComponentInfo{projeto.ufma.com.projeto/projeto.ufma.com.projeto.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process projeto.ufma.com.projeto. Make sure to call FirebaseApp.initializeApp(Context) first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
     Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process projeto.ufma.com.projeto. Make sure to call FirebaseApp.initializeApp(Context) first.
        at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.4:240)
        at com.google.firebase.database.FirebaseDatabase.getInstance(com.google.firebase:firebase-database@@16.0.5:67)
        at projeto.ufma.com.projeto.MainActivity.onCreate(MainActivity.java:60)
        at android.app.Activity.performCreate(Activity.java:7183)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6944) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 

重点是我遵循了firebase文档,我做了所有告诉我要做的事情,尽管我无法连接到firebase。

我试图测试连接,但我可以......

答案

在Gradle Scripts - > build.gradle(Project:YourProjectName)下该文件中使用play services的最新类路径buildscript - > dependencies - >

dependencies {
    classpath 'com.google.gms:google-services:4.2.0'
}

以上是关于我无法初始化firebase的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Swift 使用此代码片段为 iOS 应用程序初始化 SDK?

尝试将 Firebase 添加到 iOS 应用,但无法添加初始化代码

我无法初始化firebase

无法将数据添加到 Firebase 中的不同节点

片段中的Firebase数据不是持久的,会重新下载

我可以在 android studio 的片段上使用 Firebase 吗?