我无法初始化Firebase

时间:2019-03-24 15:26:32

标签: android firebase firebase-realtime-database

我需要连接到Firebase,我正是按照Firebase文档告诉我的,为什么必须连接到Firebase。我也放置了apply插件,看到了Firebase控制台,并做了我需要做的每一个步骤。我的代码...

    FirebaseApp.initializeApp(this);

    FirebaseDatabase database = FirebaseDatabase.getInstance();

    DatabaseReference myRef= database.getReference();

    myRef.setValue("teste");

那是我的build.code(module)

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,但我做了那里告诉我的所有事情。

我正在尝试测试连接,但是我可以...

1 个答案:

答案 0 :(得分:2)

在Gradle脚本下->该文件中的build.gradle(Project:YourProjectName) 使用最新的播放服务类路径 buildscript->依赖关系->

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