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

Posted

技术标签:

【中文标题】我得到了这个错误,但我无法解决它。 com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为类型【英文标题】:I got this error , but I can't solve it. com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type 【发布时间】:2021-09-30 05:25:29 【问题描述】:

com.google.firebase.database.DatabaseException: Can't convert an object of type java.lang.String to type,好像这个错误总是会发生但是我还是不知道怎么解决。

这是我展示 RecyclerView 的活动:

    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)

【问题讨论】:

【参考方案1】:

当您使用以下参考时:

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 对象,无需执行任何操作,您的实际代码将完美运行。

附:不要忘记删除当前存在的 String 值,否则,您将继续获得该异常。

【讨论】:

谢谢!!!男人,你帮了很多忙。现在完美运行。 很高兴听到这个消息,@kianhun4167chai

以上是关于我得到了这个错误,但我无法解决它。 com.google.firebase.database.DatabaseException:无法将 java.lang.String 类型的对象转换为类型的主要内容,如果未能解决你的问题,请参考以下文章

无法解决:customview-1.1.0

无法加载 DLL 文件

八度有趣的错误 - 无法定义函数

需要一个“Widget”类型的值,但得到一个“Null”类型的值

Xcode 无法创建配置文件

在 StoreKit 中购买产品时,我得到:“无法连接到 iTunes Store”