确保首先调用FirebaseApp.initializeApp(Context)

时间:2017-12-19 13:15:14

标签: android firebase firebase-realtime-database

它可能与其他问题完全相同,但我真的不知道这里有什么问题。在此先感谢您的帮助。

build.gradle(project)

        // 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.3.3'
        classpath 'com.google.gms:google-services:3.1.0'

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

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com" // Google's Maven repository
        }
    }
}

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

的build.gradle(模块)的

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.bustracker.usc.myapplication"
        minSdkVersion 21
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    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:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    compile 'com.google.android.gms:play-services-maps:10.2.6'
    compile 'com.google.android.gms:play-services-location:10.2.6'
    compile 'com.google.firebase:firebase-database:10.2.6'
    testCompile 'junit:junit:4.12'
}

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

MainActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FirebaseApp.initializeApp(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        firebaseDatabase = FirebaseDatabase.getInstance();
        mRootReference = firebaseDatabase.getReference();
        mheadingReference = mRootReference.child("users");

ERROR:

FATAL EXCEPTION: main
                                                 Process: com.bustracker.sample.myapplication, PID: 2658
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bustracker.sample.myapplication/com.bustracker.sample.myapplication.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.bustracker.sample.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:154)
                                                     at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                  Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.bustracker.sample.myapplication. Make sure to call FirebaseApp.initializeApp(Context) first.
                                                     at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                     at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
                                                     at com.bustracker.sample.myapplication.MainActivity.onCreate(MainActivity.java:53)
                                                     at android.app.Activity.performCreate(Activity.java:6679)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                     at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:154) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

它可能与其他问题完全相同,但我真的不知道这里有什么问题。在此先感谢您的帮助。

4 个答案:

答案 0 :(得分:11)

对我来说,任何地方都找不到解决方案。仅此有效。 只需将我的Google服务的评分从4.1.0下载到4.0.0

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.0-alpha08'
    classpath 'com.google.gms:google-services:4.0.0'
    /*classpath 'com.google.gms:google-services:4.1.0' <-- this was the problem */
}

因此,如果您更新了Google服务,请尝试降级或更改为旧版本。 希望对别人有帮助。

答案 1 :(得分:2)

根据文件:

  

正如文档中所述:

     

任何FirebaseApp初始化只能在应用程序的主进程中进行。不支持在主进程以外的进程中使用Firebase,这可能会导致与资源争用相关的问题。

您需要在活动中初始化它。

在清单示例中添加一个应用程序类:

      <applicaton
       android:name="MyApplication"

然后这样做:

public class MyApplication extends Application {
  @Override
public void onCreate() {
    super.onCreate();
   FirebaseApp.initializeApp(this);
}

并从活动中删除初始化。您需要在作为基类的应用程序类中初始化它。

编辑(关于申请):

  

用于维护全局应用程序状态的基类。您可以通过创建子类并将此子类的完全限定名称指定为AndroidManifest.xml标记中的“android:name”属性来提供您自己的实现。在创建应用程序/包的进程时,Application类或Application类的子类在任何其他类之前被实例化。

有用的链接:https://developer.android.com/reference/android/app/Application.html

答案 2 :(得分:1)

对我来说,足以确保在我的app.gradle中初始化google-services插件:

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

然后,我什至不必致电FirebaseApp.initializeApp(Context)。也许可以帮助某人。

答案 3 :(得分:0)

我不知道为什么这对我有用,但是删除

tools:node="replace" 
我的AndroidManifest.xml中的

可以将崩溃从另一种情况立即解决。希望我能帮助别人。