getContext()不存在 [英] getContext() doesn't exist

查看:136
本文介绍了getContext()不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我一直在接受官方网站上的Android Developer培训,有一点他们希望我们最终实例化我们的数据库.

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());

但是,我收到 getContext()方法的错误.它指出无法找到该方法的符号.

However, I'm getting an error for the getContext() method. It states that it cannot find a symbol for that method.

所以我搜索了源,但是在View类中找不到该方法.这是不赞成使用的方法吗?如果这不是一个选择,那么还有其他方法可以获取视图的上下文吗?

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?

谢谢!

推荐答案

您传递的代码行是:

FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(geContext());


如果您替换以下任何代码行,它应该可以工作:

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

FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());

FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getApplicationContext());

FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(this);


Context的android开发人员文档:


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:

Android上的上下文"是什么?

这篇关于getContext()不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆