首页 > 解决方案 > 在 Android Studio 中运行时,Android 应用程序停止在模拟器上运行

问题描述

大家好,我正在尝试将 firebase 添加到我的应用程序中,但在运行时停止在 Emulator 上工作

我的日志:

03-21 09:14:40.581 6568-6568/com.example.fireproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fireproject, PID: 6568
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.fireproject. Make sure to call FirebaseApp.initializeApp(Context) first.
    at com.google.firebase.FirebaseApp.getInstance(SourceFile:218)
    at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source)
    at com.example.fireproject.MainActivity$1.onClick(MainActivity.java:31)
    at android.view.View.performClick(View.java:4756)
    at android.view.View$PerformClick.run(View.java:19749)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5221)
    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)

标签: androidfirebasefirebase-realtime-database

解决方案


首先尝试使用支持的模拟器google-play-services,检查这个


同样正如崩溃所解释的,请尝试致电

FirebaseApp.initializeApp(Context)

在你的活动中使用firebase实例之前,你可以在你的ApplicationonCreate方法中这样:

FirebaseApp.initializeApp(getBaseContext())

并确保您正确应用最新 google-play-services插件


推荐阅读