android - 我无法初始化火力基地
问题描述
我需要连接到firebase,我完全按照firebase 文档告诉我的原因,为什么我必须连接到firebase。我也放了应用插件,我看到了 firebase 控制台,我做了我需要做的每一个步骤。我的代码...
FirebaseApp.initializeApp(this);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef= database.getReference();
myRef.setValue("teste");
那是我的 build.code(module)
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "projeto.ufma.com.projeto"
minSdkVersion 15
targetSdkVersion 28
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:28.0.0-rc01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.4'
implementation 'com.google.firebase:firebase-database:16.0.5'
}
但总是我得到那个错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: projeto.ufma.com.projeto, PID: 28861
java.lang.RuntimeException: Unable to start activity ComponentInfo{projeto.ufma.com.projeto/projeto.ufma.com.projeto.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process projeto.ufma.com.projeto. Make sure to call FirebaseApp.initializeApp(Context) first.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process projeto.ufma.com.projeto. Make sure to call FirebaseApp.initializeApp(Context) first.
at com.google.firebase.FirebaseApp.getInstance(com.google.firebase:firebase-common@@16.0.4:240)
at com.google.firebase.database.FirebaseDatabase.getInstance(com.google.firebase:firebase-database@@16.0.5:67)
at projeto.ufma.com.projeto.MainActivity.onCreate(MainActivity.java:60)
at android.app.Activity.performCreate(Activity.java:7183)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
关键是我遵循了firebase文档,我做了那里告诉我要做的一切,尽管我无法连接到firebase。
我试图测试连接,但我只能......
解决方案
在 Gradle Scripts -> build.gradle(Project:YourProjectName) 下,该文件中使用 play services buildscript -> dependencies -> 的最新类路径
dependencies {
classpath 'com.google.gms:google-services:4.2.0'
}
推荐阅读
- azure - PowerBI - 服务主体访问
- typescript - 打字稿:对象可能是“未定义”,但仅适用于“<”和“>”运算符
- python - 如何使用分水岭去除脑MRI中的头骨
- swift - 如何在不冻结所有应用程序的情况下使用 sleep in while?
- python - 确定白国王是否在棋盘上给定位置的布尔函数
- usb - 是否有设备可以通过 USB 端口为我的计算机提供 gpio 引脚?
- javascript - 如何在不编写冗余代码的情况下创建多个 Html 子页面?
- python - 编写一个随机数生成器,它基于 0 和 1 之间的均匀分布数,从 Lévy 分布中采样?
- javascript - 固定位置侧菜单的javascript问题
- mysql - 我可以引用之前在 MySQL select 语句中创建的变量吗?