这里是错误,每当我试图进入另一个活动,提供数据库信息,它显示:
2022-12-20 17:58:40.090 7261-7261/com.example.blood E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.blood, PID: 7261
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.blood.utilities.User
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.blood.activities.NewsfeedActivity$getUserData$1.onDataChange(NewsfeedActivity.kt:43)
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:236)
at android.app.ActivityThread.main(ActivityThread.java:8057)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)
我的代码:
class NewsfeedActivity:AppCompatActivity () {
private lateinit var dbref: DatabaseReference
private lateinit var userRecyclerView: RecyclerView
private lateinit var userArrayList: ArrayList<User>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_newsfeed)
userRecyclerView = findViewById(R.id.userList)
userRecyclerView.layoutManager = LinearLayoutManager(this)
userRecyclerView.setHasFixedSize(true)
userArrayList = arrayListOf<User>()
getUserData()
}
private fun getUserData() {
dbref = FirebaseDatabase.getInstance().getReference("Users")
dbref.addValueEventListener(object : ValueEventListener{
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()){
for (userSnapshot in snapshot.children){
val user = userSnapshot.getValue(User::class.java)
userArrayList.add(user!!)
}
userRecyclerView.adapter=MyAdapter(userArrayList)
}
}
override fun onCancelled(error: DatabaseError) {
finish()
}
})
}
用户数据类代码我使用这些:
data class User(
val name: String? = null,
val email: String? = null,
val phoneNumber: String? = null,
val bloodGroup: String? = null,
val cityName: String? = null,
val availabality: String? = null
)
以前我试过将Data类的所有参数设置为null。
1条答案
按热度按时间8yoxcaq71#
看起来数据库中
/Users
下的某个值是一个普通字符串值,而不是User
对象。请在Firebase控制台中查看您的数据,看看是哪个子对象。