猿问

DatabaseException 的运行时错误:

我收到错误com.google.firebase.database.DatabaseException: Failed to convert value of type java.lang.Long to String in line 53


这条线是var note = n.getValue(Note::class.java)


我已经被这个错误困住了一段时间,因为我对编码还不够新,我不知道该怎么做,感谢任何帮助,非常感谢


package com.example.gearoidodonovan.books


import android.app.AlertDialog

import android.support.v7.app.AppCompatActivity

import android.os.Bundle

import android.renderscript.Sampler

import android.widget.Toast

import com.google.firebase.database.*

import kotlinx.android.synthetic.main.activity_main.*

import kotlinx.android.synthetic.main.add_note.view.*

import java.text.SimpleDateFormat

import java.util.*



class MainActivity : AppCompatActivity() {


    var mRef:DatabaseReference? = null

    var mNoteList:ArrayList<Note>?= null


    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)

        setContentView(R.layout.activity_main)



        val database = FirebaseDatabase.getInstance()

        mRef = database.getReference("Notes")

        mNoteList = ArrayList()




        add_new_note.setOnClickListener {


            showDialogAddNote()



        }

    }


    override fun onStart(){

        super.onStart()

        mRef?.addValueEventListener(object : ValueEventListener{

            override fun onCancelled(p0: DatabaseError) {

            }


            override fun onDataChange(p0: DataSnapshot) {

                for (n in p0!!.children) {

                    var note = n.getValue(Note::class.java)

                    mNoteList?.add(note!!)


                }


                val noteAdapter = NoteAdapter(applicationContext, mNoteList!!)

                note_list_view.adapter = noteAdapter


            }


        })


    }


qq_花开花谢_0
浏览 92回答 2
2回答

翻翻过去那场雪

您的订单属性class note如下var id:String?= nullvar note:long?= nullvar timestamp: String? = nullvar title:String? = null```

qq_笑_17

您收到此错误是因为您的 firebase 节点中有一个 long 类型的字段,您将其保存在 String 中。确保数据类型与 firebase 节点和您的数据类中的数据类型相同。我认为大多数情况下 id 的数据类型都是 long ,您可以检查并据此更改 Note 的数据类
随时随地看视频慕课网APP

相关分类

Java
我要回答