kotlin 我得到了这个错误,但我不能解决它,com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为

6ss1mwsb  于 2023-04-21  发布在  Kotlin
关注(0)|答案(1)|浏览(155)

com.google.firebase.database.DatabaseException:无法将类型为java.lang.String的对象转换为类型,似乎这个错误总是发生,但我仍然不知道如何解决它。
这是我用来显示RecyclerView的Activity:

package com.example.budget

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.ImageButton
import android.widget.TextView
import androidx.drawerlayout.widget.DrawerLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.budget.data.bill
import com.example.budget.login_register.LoginActivity
import com.google.firebase.database.*
import com.google.android.material.navigation.NavigationView
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.database.DatabaseReference

class TransactionActivity : AppCompatActivity(),NavigationView.OnNavigationItemSelectedListener {
    lateinit var drawer_layout: DrawerLayout
    lateinit var ref: DatabaseReference
    lateinit var database: FirebaseDatabase
    lateinit var nav_view: NavigationView
    lateinit var btnMenu: ImageButton
    var isbtnMenuClicked: Boolean = false
    var id: Int = 0
    var strName =""
    var strEmail = ""
    lateinit var nav_header : View
    lateinit var tvName: TextView
    lateinit var tvEmail: TextView
    lateinit var auth: FirebaseAuth
    lateinit var list_bill : ArrayList<bill>
    lateinit var billRecycleView : RecyclerView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_transaction)

    billRecycleView = findViewById(R.id.list_bill)
    database = FirebaseDatabase.getInstance()
    auth = FirebaseAuth.getInstance()
    drawer_layout = findViewById(R.id.drawer_layout)
    nav_view = findViewById(R.id.nav_view)
    btnMenu = findViewById(R.id.btnMenu)
    nav_header = nav_view.getHeaderView(0)
    tvName = nav_header.findViewById(R.id.tvName)
    tvEmail = nav_header.findViewById(R.id.tvEmail)
    setUsername()
    setEmail()

    nav_view.setNavigationItemSelectedListener(this)
    btnMenuClicked()
    auth= FirebaseAuth.getInstance()
    billRecycleView.layoutManager= LinearLayoutManager(this)
    billRecycleView.setHasFixedSize(true)
    list_bill = arrayListOf<bill>()
    getTransaction()

}

fun getTransaction(){
    val uid = auth.currentUser!!.uid
    ref = database.getReference("Bill").child(uid)
    ref.addValueEventListener(object : ValueEventListener{
        override fun onDataChange(snapshot: DataSnapshot) {
            if (snapshot.exists()){
                for (data in snapshot.children){
                    val model = data.getValue(bill::class.java)
                    list_bill.add(model!!)
                }
                billRecycleView.adapter = Myadpater(list_bill)
            }
        }

        override fun onCancelled(error: DatabaseError) {
            TODO("Not yet implemented")
        }

    })
}

这是Myadapter类

class Myadpater(val list_bill: ArrayList<bill>) :
    RecyclerView.Adapter<Myadpater.MyViewHolder>() {
    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val category: TextView = itemView.findViewById(R.id.TCategory)
        val expense: TextView = itemView.findViewById(R.id.TExpense)
        val date :TextView = itemView.findViewById(R.id.TDate)

    }
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
    val itemView =
        LayoutInflater.from(parent.context).inflate(R.layout.transaction_history, parent, false)
    return MyViewHolder(itemView)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    holder.category.text = list_bill[position].category
    holder.expense.text = list_bill[position].expense
    holder.date.text = list_bill[position].date
}

override fun getItemCount(): Int {
    return list_bill.size
}

}

class Myadpater(val list_bill: ArrayList<bill>) :
       RecyclerView.Adapter<Myadpater.MyViewHolder>() {
       class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
         val category: TextView = itemView.findViewById(R.id.TCategory)
         val expense: TextView = itemView.findViewById(R.id.TExpense)
         val date :TextView = itemView.findViewById(R.id.TDate)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.transaction_history, parent, false)
        return MyViewHolder(itemView)
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.category.text = list_bill[position].category
        holder.expense.text = list_bill[position].expense
        holder.date.text = list_bill[position].date
    }

    override fun getItemCount(): Int {
        return list_bill.size
    }

}

enter image description here
这是我得到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.budget, PID: 5201
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.budget.data.bill
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:436)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
    at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
    at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
    at com.example.budget.TransactionActivity$getTransaction$1.onDataChange(TransactionActivity.kt:71)
    at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
    at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
    at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
g6baxovj

g6baxovj1#

当您使用以下引用时:

ref = database.getReference("Bill").child(uid)

并使用以下循环遍历DataSnapshot:

for (data in snapshot.children){
    val model = data.getValue(bill::class.java)
    list_bill.add(model!!)
}

这意味着你试图获取UID节点中存在的每个子节点,并将其转换为model类型的对象。看到你的截图,我可以说UID节点下没有model对象,只有String对象。由于Kotlin中没有方法可以将String转换为model类型的对象,如果你想在UID节点下有多个model对象,那么你应该在每次添加数据时使用push():

ref.push().setValue(yourModelObj).addOnCompleteListener(/* ... /*)
//  ^^^^^

您的新数据库模式将如下所示:

Firebase-root
  |
  --- Bill
       |
       --- KoqT...O6H3
             |
             --- $pushedId //Newly added level
                    |
                    --- categoryy: "food"
                    |
                    --- date: "22 Jul, 2021"
                    |
                    --- expense: "20"

要取回所有的model对象,没有什么可做的,您的实际代码将完全正常工作。
P.S.不要忘记删除现在存在的String值,否则,您将继续获得该Exception。

相关问题