我收到此错误,但我无法解决。 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
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")
}
})
}
这是我的适配器class
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.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.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)
当您使用以下参考时:
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 中无法将字符串转换为 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。不要忘记删除现在存在的字符串值,否则,您将继续获得该异常。
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")
}
})
}
这是我的适配器class
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.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.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)
当您使用以下参考时:
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 中无法将字符串转换为 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。不要忘记删除现在存在的字符串值,否则,您将继续获得该异常。