异常使用Firebase时无法启动活动ComponentInfo

时间:2018-03-08 23:49:48

标签: android firebase firebase-realtime-database

我试图遵循使用Firbase数据库的教程,我不知道为什么会发生这种情况,因为这是我第一次尝试使用它。

我在我的app logcat中收到此异常。

03-09 02:39:57.373 15997-15997/com.example.android.clouddatabase E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.android.clouddatabase, PID: 15997
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.clouddatabase/com.example.android.clouddatabase.MainActivity}: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2491)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564)
                                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:170)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                   at android.os.Looper.loop(Looper.java:194)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5576)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750)
                                                                                Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
                                                                                   at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                                   at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                                   at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                                                   at com.example.android.clouddatabase.MainActivity.onCreate(MainActivity.java:46)
                                                                                   at android.app.Activity.performCreate(Activity.java:6041)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2437)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2564) 
                                                                                   at android.app.ActivityThread.access$800(ActivityThread.java:170) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1441) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                   at android.os.Looper.loop(Looper.java:194) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5576) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:955) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:750) 

这是我的build.gradle应用程序文件,如

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.android.clouddatabase"
        minSdkVersion 15
        targetSdkVersion 26
        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:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    compile 'com.android.support:design:26.1.0'
    compile 'com.google.firebase:firebase-database:9.4.0'
    compile 'com.google.firebase:firebase-auth:9.4.0'
    compile 'com.google.firebase:firebase-core:9.4.0'
    compile 'com.android.support:recyclerview-v7:26.1.0'
    compile 'com.android.support:multidex:1.0.1'


}

我尝试了很多解决方案,但似乎没有解决方案。 所以我打开任何建议

1 个答案:

答案 0 :(得分:1)

您忘了将此添加到应用级草图文件的末尾:

apply plugin: 'com.google.gms.google-services' 

请注意,在项目级别的gradle文件中,您应该:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.2.0'


    }
}

看起来你也没有配置文件,here's如何下载它。