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 8555309
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:05:42+00:00 2026-06-11T15:05:42+00:00

I am trying to use getString() to get an String from resources to assign

  • 0

I am trying to use getString() to get an String from resources to assign it to an String array before my activity is created:

private static final String[] MenuNames = {
    Resources.getSystem().getString(R.string.LCMeterMenu),
    Resources.getSystem().getString(R.string.FrecMenu),
    Resources.getSystem().getString(R.string.LogicAnalyzerMenu),
    "Prueba con achartengine",
    Resources.getSystem().getString(R.string.BrazoMenu)
};

When I use Resources.getSystem().getString(R.string.LCMeterMenu), Eclipse doesn’t complain but I get an error at runtime:

Caused by: android.content.res.Resources$NotFoundException: String Resource ID #0x7f0a000a

But if I put inside onCreate():

Log.i("StringR", "String: " + getString(R.string.LCMeterMenu));

I get the String but I can’t assign it to the final String I defined before. If I use only getString() before onCreate() I get and static error message. How can I use resources before onCreate() for global variables?

  • 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-11T15:05:43+00:00Added an answer on June 11, 2026 at 3:05 pm

    You cannot initialize a static final field from resources; the field needs to be initialized at the time the class is initialized and that happens before the application resources have been bound at run time. (By the way, the reason you cannot use Resources.getSystem() is that the Resources object you obtain that way contains only system resources, not any application resources.)

    If you need those strings available before the application resources are bound, the only practical thing to do is to put the strings into the code directly. However, the “Android way” would be to organize your code so initialization only needs to happen during (or after) onCreate(). Just initialize the string array in onCreate() and don’t worry about making the fields static or final.

    If you don’t want the string array to be associated with a particular activity, then you can subclass Application and read the array from resources inside the application class’s onCreate() method. (You also need to declare your custom application class in the manifest.) However, the docs recommend against such an approach. (Since the array is private, I suspect that it is closely tied to a single activity anyway, so the use of an Application subclass doesn’t seem warranted.)

    An alternative is to declare a singleton class for your array. The singleton accessor function then needs a Context so it can retrieve the resources if necessary:

    public class StringArray {
        private static String[] theArray;
        public static String[] getArray(Context context) {
            if (theArray == null) {
                theArray = context.getResources().getStringArray(R.array.my_strings);
            }
            return theArray;
        }
    }
    

    (This assumes the string data are defined in a <string-array> resource like @JaiSoni suggested in his answer.) Once again, the member field cannot be declared final.

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

Sidebar

Related Questions

I am trying to write a function to get a string from uart. Its
I'm trying to use SharpSSH to get a file from a remote SFTP server,
I am trying to pass a string from one activity to another. I do
I am trying to create a md5 string from the byte array of an
I'm trying to use the JSON library to consume twitter information from the get
I'm trying to get data from DB. I must use to queries. From the
I am getting some problems while trying to use this regex: (public +function|private +function|function)
I am trying to use an intent to send an email from my application
I was trying to use XTEA encryption on NETMF using the code from blog
I've been trying to use AutoMapper to save some time going from my DTOs

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.