java getContext() 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25587265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getContext() doesn't exist
提问by LalienX
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.
所以我一直在官方网站上接受 Android 开发人员培训,他们希望我们最终实例化我们的数据库。
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.
但是,我收到该getContext()
方法的错误。它指出它找不到该方法的符号。
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?
所以我搜索了源代码,但在 View 类中找不到该方法。这是不推荐使用的方法吗?如果这不是一个选项,还有其他方法可以获取视图的上下文吗?
Thank you!
谢谢!
采纳答案by lcsvcn
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:
Context 的 android 开发者文档:
https://developer.android.com/reference/android/content/Context.html
https://developer.android.com/reference/android/content/Context.html
You might found helpful too look in this question, that explains what is Context for:
您可能会发现在这个问题中也很有帮助,它解释了什么是上下文:
回答by Luis
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; }
主要活动
FeedReaderContract contract = new FeedReaderContract(this);
我编辑了类 FeedReaderContract 的构造函数
mDbHelper = new FeedReaderDbHelper(getContext());
方法 getContext()
公共上下文 getContext() { 返回上下文;}
回答by Nikhil Jain
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
在你的代码已经使用geContext()将其更改为getContext()
或getApplicationContext()
,或者调用从活动对象里面即速this
回答by Eran
The View class does have a getContextmethod.
View 类确实有一个getContext方法。
You either have a typo, or your code is not located in a non-static method of a sub-class of View.
您要么有错别字,要么您的代码未位于 View 子类的非静态方法中。