getContext() doesn't exist

前端 未结 4 1754
Happy的楠姐
Happy的楠姐 2021-02-20 12:29

So I have been going through the Android Developer training on the official site and there is a point where they want us to finally instantiate our database.

So they te

相关标签:
4条回答
  • 2021-02-20 12:45

    The line of code you pass is:

    FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(geContext());
    


    It should work if you substitute for any of these code lines :

    FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
    

    Or

    FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getApplicationContext());
    

    Or

    FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(this);
    


    The android developer documentation of the Context:

    https://developer.android.com/reference/android/content/Context.html

    You might found helpful too look in this question, that explains what is Context for:

    What is 'Context' on Android?

    0 讨论(0)
  • 2021-02-20 12:56

    Thats how I made it

    1. MainActivity

      FeedReaderContract contract = new FeedReaderContract(this);

    2. I edited the constructor of the class FeedReaderContract

      mDbHelper = new FeedReaderDbHelper(getContext());

    3. The method getContext()

      public Context getContext() { return context; }

    0 讨论(0)
  • 2021-02-20 13:00

    In your code you have used geContext() change it to getContext() or getApplicationContext() or if calling the object from inside an activity simply pass this

    0 讨论(0)
  • 2021-02-20 13:03

    The View class does have a getContext method.

    You either have a typo, or your code is not located in a non-static method of a sub-class of View.

    0 讨论(0)
提交回复
热议问题