Home > Blockchain >  Whenever I click an activity ,app is get crushed or sent back user to previous activity.What is The
Whenever I click an activity ,app is get crushed or sent back user to previous activity.What is The

Time:12-21

Here is the error , whenever I tried to enter another activity which provide database information it shows :

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)

My Code:

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()
            }
        })
    }

User Data Class Code Where I am using these:

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
)

Previously I tried the set all the argument of Data class at null.

CodePudding user response:

It looks like one of the values under /Users in your database is a plain string value, rather than a User object. Have a look at your data in the Firebase console to see which child that is.

  • Related