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 tell us to use this snippet of code:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
However, I'm getting an error for the getContext()
method. It states that it cannot find a symbol for that method.
So I searched the source and that method in the View class just cannot be found. Is this a deprecated method? And if this isn't an option, is there any other way we can grab the context of a view?
Thank you!
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:
Thats how I made it
MainActivity
FeedReaderContract contract = new FeedReaderContract(this);
I edited the constructor of the class FeedReaderContract
mDbHelper = new FeedReaderDbHelper(getContext());
The method getContext()
public Context getContext() { return context; }
在您的代码中,您使用了geContext()将其更改为getContext()
或getApplicationContext()
或者如果从活动内部调用该对象,则只需传递this
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.