How to initialize context? [closed]

天大地大妈咪最大 提交于 2019-12-12 13:23:59

问题


This is a really stupid question but what do I set context equal to in Context context = ...

Just declaring it sets it to null and I need to use context for my app.


回答1:


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:

  1. Assign a pointer to an Activity object to the Context object pointer (i.e. Using the this keyword)

  2. The Activity class has a method called getApplicationContext() which retrieves a pointer to the Context object contained by the Application.



来源:https://stackoverflow.com/questions/14948362/how-to-initialize-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!