Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get resources inside a static context block?

I want to create a static Hash table to convert strings to integers. The caveat here is that I want to use strings I defined as resources in several lists in XML files.

I am able to write this, using only resources IDs:

public class MyActivity extends Activity {

private static Map<Integer, Integer> views = new HashMap<Integer, Integer>();
static {
    views.put(R.string.full_text, MessageTable.VIEW_FULL);
    views.put(R.string.only_text, MessageTable.VIEW_MSG);
    views.put(R.string.tag_text, MessageTable.VIEW_TAGMSG);
}

I get no errors, but what I would really need to do is something like this:

public class MyActivity extends Activity {

private static Map<String, Integer> views = new HashMap<String, Integer>();
static {
    views.put(getResources().getString(R.string.full_text), MessageTable.VIEW_FULL);
    views.put(getResources().getString(R.string.only_text), MessageTable.VIEW_MSG);
    views.put(getResources().getString(R.string.tag_text), MessageTable.VIEW_TAGMSG);
}

which gives me the following error in Eclipse:

Cannot make a static reference to the non-static method getResources() from the type ContextWrapper

The message makes sense, but what does not makes sense is that resources are unreachable from the static block, one would think static variables were custom created to make use of resources.
I guess I can populate the Hash table during the object constructor, but this would mean to do it in the wrong place.

like image 523
ilomambo Avatar asked Apr 17 '12 16:04

ilomambo


2 Answers

getResources() (short for ~ MyActivity.this.getResources()) requires a context object which is not initialized at that time. Since context is only available after you hit onCreate you can't even do it at construction time of MyActivity.

The reason is that the activity manager which instantiates your MyActivity class has to determine the configuration (orientation, screen size, language, ...) before he knows which resources have to be extracted from the xml. -> Resources are not static and can't be accessed from static context.

So I guess there is no way around doing those operations in onCreate or later.

Edit: While you certainly can update the static HashMap (or a static Context) from onCreate I wouldn't recommend that since you can have several instances of the same Activity with possibly different / changing configurations. Also storing a static Context is a great way to create a Memory Leak

like image 82
zapl Avatar answered Sep 28 '22 01:09

zapl


public Static Resources mResources;
public MyApplication extends Application
{
     @Override
     public void onCreate()
     {
           mResources = getResources();
     }

}

Once you have the static reference to the Resources, you can refer to it anywhere from the entire application. However i am not sure about whether this is gonna introduce a momory leak or not;

like image 43
Khurram Shehzad Avatar answered Sep 28 '22 01:09

Khurram Shehzad