title: Is it possible to get resources inside a static context block?
body: <p>I want to create a <strong>static</strong> Hash table to convert <em>strings</em> to <em>integers</em>. The caveat here is that I want to use <em>strings</em> I defined as <strong>resources</strong> in several lists in <strong>XML files</strong>.</p>
  
  <p>I am able to write this, using only resources IDs:</p>
  
  <pre class="lang-js prettyprint-override"><code>public class MyActivity extends Activity {
  
  private static Map&lt;Integer, Integer&gt; views = new HashMap&lt;Integer, Integer&gt;();
  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);
  }
  </code></pre>
  
  <p>I get no errors, but what I would really need to do is something like this:</p>
  
  <pre class="lang-js prettyprint-override"><code>public class MyActivity extends Activity {
  
  private static Map&lt;String, Integer&gt; views = new HashMap&lt;String, Integer&gt;();
  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);
  }
  </code></pre>
  
  <p>which gives me the following error in Eclipse:</p>
  
  <pre class="lang-none prettyprint-override"><code>Cannot make a static reference to the non-static method getResources() from the type ContextWrapper
  </code></pre>
  
  <p>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.<br>
  I guess I can populate the Hash table during the object constructor, but this would mean to do it in the wrong place.</p>