在这个进程com.example.app中,我们看到了一些异常消息Default FirebaseApp is not initialized。确保首先调用FirebaseApp.initializeApp(Context)。在我们的Android应用中,我们刚刚添加了Firebase远程配置。

堆栈跟踪如下:

Fatal Exception: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.app. Make sure to call FirebaseApp.initializeApp(Context) first.
       at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
       at com.google.firebase.remoteconfig.FirebaseRemoteConfig.getInstance(Unknown Source)
       at com.example.app.fragments.SomeFragment.updateFooter(SourceFile:295)
       at com.example.app.fragments.SomeFragment.onCreateView(SourceFile:205)
       at android.support.v4.app.Fragment.performCreateView(SourceFile:2080)
       at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1108)
       at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1290)
       at android.support.v4.app.BackStackRecord.run(SourceFile:801)
       at android.support.v4.app.FragmentManagerImpl.execSingleAction(SourceFile:1638)
       at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(SourceFile:679)
       at android.support.v4.app.FragmentPagerAdapter.finishUpdate(SourceFile:143)
       at android.support.v4.view.ViewPager.populate(SourceFile:1240)
       at android.support.v4.view.ViewPager.populate(SourceFile:1088)
       at android.support.v4.view.ViewPager.setAdapter(SourceFile:542)
       at com.example.app.SomeActivity.onSomeAsyncCallback(SourceFile:908)
       at com.example.app.SomeDataRetriever.onAsyncHttpCompleted(SourceFile:72)
       at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:141)
       at com.example.app.io.AsyncHttp.onPostExecute(SourceFile:19)
       at android.os.AsyncTask.finish(AsyncTask.java:679)
       at android.os.AsyncTask.access$500(AsyncTask.java:180)
       at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:696)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:150)
       at android.app.ActivityThread.main(ActivityThread.java:5665)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:799)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:689)

这是9.6.1版本,我们还使用了其他Firebase组件:

compile 'com.google.firebase:firebase-ads:9.6.1'
compile 'com.google.firebase:firebase-config:9.6.1'
compile 'com.google.firebase:firebase-invites:9.6.1'
compile "com.google.firebase:firebase-messaging:9.6.1"

正如我从文档和Javadoc中看到的,在我们的例子中,我们不应该做任何手动初始化。

这个异常发生在Android 4-6上的各种设备上。

编辑:

我发现这个问题引起了一些关注。我想这个解释对你们中的一些人来说可能很有趣:https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html


当前回答

正如@PSIXO在评论中提到的,这可能是google-services依赖版本的问题。为了我的改变,

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.1.0'
    }
}

to

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.0.1'
    }
}

工作。4.1.0版本可能有一些问题。因为我在这个问题上浪费了很多时间,所以我想把这个作为一个答案。

其他回答

我解决这个问题: 应用插件:'com.google.gms。谷歌服务正在建设中。Gradle文件并移动文件google-service。Json在app文件夹下。

单击“工具> Firebase”,打开“助手”窗口。

单击以展开列出的功能之一(例如,Analytics),然后单击提供的教程链接(例如,记录Analytics事件)。

单击“连接到Firebase”按钮以连接到Firebase并将必要的代码添加到应用程序中。

https://firebase.google.com/docs/android/setup

确保添加到你的根级build.gradle

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.3.2'
    }
}

然后,在你的模块级Gradle文件(通常是app/build.gradle)中,在文件底部添加'apply plugin'行来启用Gradle插件:

apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  implementation 'com.google.firebase:firebase-core:9.6.1'
  // Getting a "Could not find" error? Make sure you have
  // the latest Google Repository in the Android SDK manager
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

如文件中所述。在上面的一个问题中,当我忘记在我的gradle文件中添加这个时,我有例外。

通过Android Studio Tools安装Firebase…

我通过Android Studio的内置工具进行安装(遵循Firebase的最新文档)。 这安装了基本的依赖项,但当我试图连接到数据库时,它总是给我一个错误,我需要首先调用初始化,即使我是:

默认的FirebaseApp在此过程中没有初始化。一定要 首先调用FirebaseApp.initializeApp(Context)。

无论我做什么,都会得到这个错误。

最后,在看到其他答案中的一个评论后,我把我的gradle从4.1.0版本更改为:

classpath 'com.google.gms:google-services:4.0.1'

当我这样做的时候,我终于看到了一个帮助我的错误:

File google-services.json is missing. The Google Services Plugin cannot function without it. Searched Location: C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\debug\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\nullnull\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnull\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\debug\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\src\nullnullDebug\google-services.json C:\Users\%username%\AndroidStudioProjects\TxtFwd\app\google-services.json

这就是问题所在。由于某些原因,4.1.0版本似乎没有给出这个构建错误——没有提到你有一个丢失的google服务。json文件。我没有谷歌的服务。json文件,所以我出去添加了它。

但由于这是一个升级,使用现有的实时first base数据库,我从来没有生成该文件在过去。 我去firebase生成并添加了它,它解决了这个问题。

更改回4.1.0

一旦我发现了所有这些,然后我把类路径变量改回来(到4.1.0),重新构建,它再次崩溃,错误是它没有被初始化。

根源问题

在预编译时,使用4.1.0构建不会为您提供有效的错误,因此您可能不知道发生了什么。 在4.1.0上运行会导致初始化错误。

我在我的app/build中漏掉了下面这一行。gradle文件

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

并且一旦清理项目并再次运行。这为我解决了问题。