为什么Firebase会导致应用崩溃

时间:2016-11-02 01:32:20

标签: android firebase firebase-realtime-database

Project Gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

App Gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
        applicationId "com.myapp"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        packagingOptions {
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/LICENSE-FIREBASE.txt'
            exclude 'META-INF/NOTICE'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.android.support:support-v4:24.2.1'
    //compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.google.android.gms:play-services:9.8.0'
    compile 'com.google.firebase:firebase-core:9.8.0'

    testCompile 'junit:junit:4.12'
}

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

的活动:

private DatabaseReference mDatabase;
...
mDatabase = FirebaseDatabase.getInstance().getReference(Config.FIREBASE_URL); //public static final String FIREBASE_URL = "https://myapp.firebaseio.com/ ";
...
mDatabase.setValue(edittextName.getText().toString());

类别:

public class Config {
    public static final String FIREBASE_URL = "https://myapps.firebaseio.com/ ";
}

我收到以下错误:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.myapp, PID: 30211
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp/com.myapp.MainActivity}: com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: FirebaseApp object has no DatabaseURL in its FirebaseOptions object.
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2316)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376)
                  at android.app.ActivityThread.access$800(ActivityThread.java:147)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5253)
                  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:899)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
               Caused by: com.google.firebase.database.DatabaseException: Failed to get FirebaseDatabase instance: FirebaseApp object has no DatabaseURL in its FirebaseOptions object.
                  at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                  at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                  at com.myapp.MainActivity.onCreate(MainActivity.java:103)
                  at android.app.Activity.performCreate(Activity.java:5975)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2376) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:147) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5253) 
                  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:899) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

如何解决问题。

提供的链接无法解决我的问题!

1 个答案:

答案 0 :(得分:0)

根据@qbix,我不得不使用getReferenceFromUrl()并且应用程序没有崩溃。