Skip to content Skip to sidebar Skip to footer

How To Initialize Context?

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.

Solution 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.

Post a Comment for "How To Initialize Context?"