Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7797781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:47:48+00:00 2026-06-01T23:47:48+00:00

I want to create a static Hash table to convert strings to integers .

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:47:50+00:00Added an answer on June 1, 2026 at 11:47 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to create a trial webpage with static languages that expires after 10
So I want to create a text box static if possible that when my
I want to create a page with, for example, static text and loop that
I want to create a static ReadOnlyCollection with strings. Is there any way to
I want to create a static executable that will run on a windows machine
I want to create a customized UIView class (a static lib) for some app
I have created a public static class utils.cs I want to use it in
I have a static table view, created using a storyboard. I want to get
I am trying ebook reading app. In that I want create UIActionSheet by clicking
I want to create an static drawing (say any animals like giraffe) using some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.