首页 > 解决方案 > 我无法初始化火力基地

问题描述

我需要连接到firebase,我完全按照firebase 文档告诉我的原因,为什么我必须连接到firebase。我也放了应用插件,我看到了 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。

我试图测试连接,但我只能......

标签: androidfirebasefirebase-realtime-database

解决方案


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

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

推荐阅读