如何初始化上下文? [英] How to initialize context?
问题描述
这是一个非常愚蠢的问题,但我该怎么在上下文的背景下设置的上下文等于= ...
This is a really stupid question but what do I set context equal to in Context context = ...
刚刚宣布它设置为null,我需要使用上下文我的应用程序。
Just declaring it sets it to null and I need to use context for my app.
推荐答案
您不能实例化一个Context对象。这是由系统控制。但是,每个应用程序都有一个上下文,每个活动是一个语境,让你有一对夫妇的是获得一个指向Context对象:
You cannot instantiate a Context object. This is controlled by the system. However, every Application has a Context and every Activity is a Context, so you have a couple of was to obtain a pointer to a Context object:
-
指定一个指向一个活动对象到上下文对象的指针(即使用this关键字)
Assign a pointer to an Activity object to the Context object pointer (i.e. Using the this keyword)
Activity类有一个名为getApplicationContext()方法,检索指向由应用程序包含的上下文对象。
The Activity class has a method called getApplicationContext() which retrieves a pointer to the Context object contained by the Application.
这篇关于如何初始化上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!