0

Working with room database. I've made a class UserApp.kt to connect my Database to app. this class working good in activity.kt. But when I put the code inside Fragment.kt class, red line error showing under application text :

Unresolved reference: application

UserApp.kt

import android.app.Application

class UserApp: Application() {

val db by lazy {
    UserDatabase.getInstance(this)
}

}

In activity the code like this:

val userDao = (application as UserApp).db.userDao()
getUsersFromRoom(userDao)

But in fragment the code like this:

But in fragment the code like this:

AI Shakil
  • 1,015
  • 10
  • 20

1 Answers1

0

I've solved the issue like this:

val db by lazy {
    UserDatabase.getInstance(requireContext())
}
val dao = db.categoryDao()
AI Shakil
  • 1,015
  • 10
  • 20