Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 读取和写入Firebase实时数据库-强制关闭_Android_Firebase_Kotlin - Fatal编程技术网

Android 读取和写入Firebase实时数据库-强制关闭

Android 读取和写入Firebase实时数据库-强制关闭,android,firebase,kotlin,Android,Firebase,Kotlin,我正在尝试创建一个android应用程序来练习firebase。我正在尝试将数据读写到firebase实时数据库。我已经按照文档进行了设置,使用下面的代码片段时效果良好: // Write a message to the database val database = Firebase.database val myRef = database.getReference("message") myRef.setValue("Hello, World!"

我正在尝试创建一个android应用程序来练习firebase。我正在尝试将数据读写到firebase实时数据库。我已经按照文档进行了设置,使用下面的代码片段时效果良好:

// Write a message to the database
val database = Firebase.database
val myRef = database.getReference("message")

myRef.setValue("Hello, World!")
通过执行以下操作,我成功地读取了
“message”
参考中的值:

// Read from the database
myRef.addValueEventListener(object : ValueEventListener {
    override fun onDataChange(dataSnapshot: DataSnapshot) {
        // This method is called once with the initial value and again
        // whenever data at this location is updated.
        val value = dataSnapshot.getValue<String>()
        Log.d(TAG, "Value is: $value")
    }

    override fun onCancelled(error: DatabaseError) {
        // Failed to read value
        Log.w(TAG, "Failed to read value.", error.toException())
})
这是logcat:

2021-05-06 00:03:05.297 25802-25802/com.dicoding.firebasepractice E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.dicoding.firebasepractice, PID: 25802
    com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(CustomClassMapper.java:161)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$200(CustomClassMapper.java:48)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.serialize(CustomClassMapper.java:676)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(CustomClassMapper.java:168)
        at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToPlainJavaTypes(CustomClassMapper.java:61)
        at com.google.firebase.database.DatabaseReference.setValueInternal(DatabaseReference.java:282)
        at com.google.firebase.database.DatabaseReference.setValue(DatabaseReference.java:159)
        at com.dicoding.firebasepractice.MainActivity$onCreate$1.onClick(MainActivity.kt:34)
        at android.view.View.performClick(View.java:7504)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View.performClickInternal(View.java:7476)
        at android.view.View.access$3600(View.java:824)
        at android.view.View$PerformClick.run(View.java:28661)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:239)
        at android.app.ActivityThread.main(ActivityThread.java:8142)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015)
2021-05-06 00:03:05.345 25802-25802/? I/Process: Sending signal. PID: 25802 SIG: 9

使用以下代码行时:

database.child("name").setValue(editText.text)
这意味着您正试图将“text”对象写入数据库,这实际上是不可能的。使用属性访问语法时,“text”对象来自EditText的类方法。如您所见,此方法返回类型为的对象。无法在数据库中写入此对象,因为它不是数据库。因此,为了能够编写与正在使用的editText相对应的文本,需要编写对象的字符串表示形式。正如@cutiko在他的评论中提到的,只需在“text”对象上调用“toString()”方法即可:

database.child("name").setValue(editText.text.toString())

共享来自的崩溃报告logcat@DeepakGoyal我刚刚编辑了这个问题,包含了这个
数据库.child(“name”).setValue(editText.text.toString())
editText.text.toString()
会起作用的,布莱恩:)我能帮你了解其他信息吗?如果你认为我的答案对你有帮助,请考虑通过点击复选来接受它。✔️) 在投票箭头下方的左侧。应该将颜色更改为绿色。我非常感谢。谢谢!
database.child("name").setValue(editText.text.toString())