gpt4 book ai didi

java - Android应用程序无法连接到firebase数据库

转载 作者:行者123 更新时间:2023-12-02 05:58:36 25 4
gpt4 key购买 nike

我正在尝试将写入数据连接到我的 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");
}
}
});
}
}

gradle文件


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
}

应用程序/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'
}

最佳答案

无需调用:

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

关于java - Android应用程序无法连接到firebase数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55991824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com