我正在尝试将写入数据连接到我的Firebase数据库中,但是它显示错误,尽管我已经初始化了Firebase,但尚未初始化。我已经尝试过更改依赖项等,但仍然无法正常工作
package com.parse.starter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
FirebaseApp.initializeApp(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseApp.initializeApp(this);
DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
Map<String,String> values=new HashMap<>();
values.put("name","Jeff");
ref.push().setValue(values,new DatabaseReference.CompletionListener()
{
@Override
public void onComplete(@Nullable DatabaseError databaseError, @NonNull DatabaseReference databaseReference) {
if(databaseError==null)
{
Log.i("info","Get to job");
}
else
{
Log.i("info","Stay here");
}
}
});
}
}
渐变文件
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app / gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.parse.starter"
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.google.firebase:firebase-database:16.1.0'
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.3'
}
答案 0 :(得分:0)
无需致电:
FirebaseApp.initializeApp(this);
如果您按照有关如何add Firebase to your Android project的官方文档中的说明执行了集成。
如果您进行了上述标准集成,则Firebase将在您的应用启动时自动初始化。在这里查看有关FirebaseApp的更多详细信息。
要解决此问题,请更改以下代码行:
implementation 'com.google.firebase:firebase-core:16.0.3'
到
implementation 'com.google.firebase:firebase-core:16.0.8'
如果您没有执行以上标准的集成,则应在需要时致电initializeApp
。