【发布时间】:2020-06-04 18:20:32
【问题描述】:
我有一个问题: Firebase 数据库不工作,我不明白为什么会发生这种情况(无法将数据发布到服务器,当我按下按钮发送数据时 - 没有任何反应,数据没有保存在服务器)。
看看我的代码:
private var db = FirebaseDatabase.getInstance()
private lateinit var dbReference: DatabaseReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
dbReference = db.reference.child("member")
add_item_to_db_button.setOnClickListener {
dbReference.push()
.setValue(Member(item_name.text.toString(), item_description.text.toString()))
}
}
我的构造函数类将数据发送到数据库:
class Member {
lateinit var itemName: String
lateinit var itemDescription: String
constructor()
constructor(itemName: String, itemDescription: String) {
this.itemName = itemName
this.itemDescription = itemDescription
}
}
我的依赖项(google() 和 jcenter() 在顶级 Gradle 中启用):
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// Firebase
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-core:17.2.2'
implementation 'com.firebaseui:firebase-ui:0.6.2'
implementation 'com.google.android.gms:play-services-gcm:17.0.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'
implementation 'com.firebaseui:firebase-ui-auth:4.3.1'
}
apply plugin: 'com.google.gms.google-services'
我的XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="40dp"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<ImageView
android:layout_width="200dp"
android:src="@color/cardview_dark_background"
android:layout_marginBottom="30dp"
android:layout_height="200dp"/>
<EditText
android:layout_width="match_parent"
android:id="@+id/item_name"
android:text="Description"
android:layout_height="wrap_content"/>
<EditText
android:text="Name of product"
android:layout_width="match_parent"
android:id="@+id/item_description"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/add_item_to_db_button"
android:layout_width="match_parent"
android:text="Add"
android:layout_height="wrap_content"/>
</LinearLayout>
最后是数据库规则:
"rules": {
".read": true,
".write": true
}
我不明白为什么 db 不工作。尝试了几种方法发布在StackOverflow - 没有帮助。
【问题讨论】:
-
水不工作
-
抱歉,我更新了一个问题。数据不会发布到数据库。单击按钮后 - 没有任何反应。
-
你的项目连接到firebase了吗?
-
当然。我已连接我的项目。
-
你确定你没有错过
FirebaseApp.initializeApp(this)吗?如果您只是将其粘贴为OnCreate或 Application Class 中的第一行。
标签: android firebase android-studio kotlin firebase-realtime-database