Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to initialize object of DBHelper class which extends SqliteOpenHelper in a fragment as it takes context as arguement. #3

Open
shikhrsngh9044 opened this issue Apr 8, 2018 · 1 comment

Comments

@shikhrsngh9044
Copy link

shikhrsngh9044 commented Apr 8, 2018

Helper.kt->

class Helper(context: Context) : SQLiteOpenHelper(context, DBNAME, null, DBVERSION) {

    companion object {
        private val DBNAME = "myDb"
        private val DBVERSION = 1
    }

-->this class will take context argument but in fragment it is showing error for "context","activity","this".. i have tried all the things but not be able to resolve the error

HomeFragment.kt->

class HomeFragment : Fragment() {

    companion object {
        const val KEY = "data"
    }

    val helper = Helper(context)  //getting error-> Required:Context , Found:Context?
@meidikawardana
Copy link

I had the same problem as you. the solution is using magic variable: ctx. I called it "magic", because I dont know where it came from. It is a context, enough to initialize the db helper

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants