Issue
I am trying to access database Handler inside a fragment but its throwing me does not have companion object error. I tried database handler query and its working fine. Any help is appreciated.
Language Used
Kotlin
Code
class OneFragment : Fragment() {
companion object
{
//Error in the below context
var db=TestDatabaseHandler(Context)
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {
{
var a=db.readData()
}
}
Solution
You cannot access context
inside companion object {}
. Instead change db
to be a field to class OneFragment
like this
class OneFragment : Fragment() {
val db by lazy { TestDatabaseHandler(context!!) }
override fun onCreateView(...)
var a = db.readData()
}
}
Reference - Property delegation in Kotlin by Antonio Leiva
Answered By - Yaswant Narayan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.