DatabaseException的运行时错误:无法将类型java.lang.Long的值转换为String

时间:2019-01-23 17:22:54

标签: java android android-studio kotlin

我收到错误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

            }

        })

    }


    fun showDialogAddNote() {
        val alertBuilder = AlertDialog.Builder(this)

        val view = layoutInflater.inflate(R.layout.add_note, null)

        alertBuilder.setView(view)

        val alertDialog = alertBuilder.create()
        alertDialog.show()


        view.btnSaveNote.setOnClickListener {
            val title = view.etTitle.text.toString()
            val note = view.etNote.text.toString()

            if (title.isNotEmpty() && note.isNotEmpty()) {
                var id = mRef!!.push().key!! //reference 1

                var myNote = Note(id, title, note, getCurrentDate())
                mRef!!.child(id).setValue(myNote)
                alertDialog.dismiss()


            } else {
                Toast.makeText(this, "Empty", Toast.LENGTH_LONG).show()
            }


        }

    }

    fun getCurrentDate(): String{
        val calendar = Calendar.getInstance()
        val mdformat = SimpleDateFormat("EEEE hh:mm a ")
        val strDate = mdformat.format(calendar.time)
        return strDate
    }
}

这是我的笔记课

package com.example.gearoidodonovan.books

import com.google.firebase.database.Exclude
import com.google.firebase.database.ServerValue

class Note {
    var id:String?= null
    var title:String? = null
    var note:String?= null
    var timestamp: String? = null

    constructor(){

    }

    constructor(id: String, title: String, note: String, timestamp: String){
        this.id = id
        this.title = title
        this.note = note
        this.timestamp = timestamp
    }
}

2 个答案:

答案 0 :(得分:0)

您收到此错误,因为您在Firebase节点中有一个long类型的字段,该字段保存在String中。确保数据类型与firebase节点和您的数据类相同。

我认为在大多数情况下,id的数据类型都是long类型,您可以检查并根据该值更改Note的数据类

答案 1 :(得分:0)

您的class note的订购属性如下

var id:String?= null
var note:long?= null
var timestamp: String? = null
var title:String? = null```