0

how to set R resources array onto a string Array

I Try this, but I get an error as:

12-10 16:10:24.793: E/AndroidRuntime(22575): FATAL EXCEPTION: main
12-10 16:10:24.793: E/AndroidRuntime(22575): java.lang.OutOfMemoryError: java.lang.String[] of length 2131230728 exceeds the VM limit
12-10 16:10:24.793: E/AndroidRuntime(22575):    at com.dec.myapp.fragments.EditUserProfileFragment.onCreateView(EditUserProfileFragment.java:102)
12-10 16:10:24.793: E/AndroidRuntime(22575):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
12-10 16:10:24.793: E/AndroidRuntime(22575):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
12-10 16:10:24.793: E/AndroidRuntime(22575):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
12-10 16:10:24.793: E/AndroidRuntime(22575):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
12-10 16:10:24.793: E/AndroidRuntime(22575):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
12-10 16:10:24.793: E/AndroidRuntime(22575):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)

this is my code

String[] mCountry = new String[R.array.countries_array];

in XML

<string-array name="countries_array">
    <item>Afghanistan</item>
    <item>Albania</item>
    <item>Algeria</item>
    <item>American Samoa</item>
    <item>Andorra</item>
...
1
  • are you trying this on emulator..?? Commented Dec 10, 2014 at 8:16

2 Answers 2

3

Try to use getStringArray() function for getting string arrays..

Code :

String[] mCountry;

mCountry= getResources().getStringArray(R.array.countries_array);

References :

public String[] getStringArray (int id)
    Return the string array associated with a particular resource ID. 

Refer this for more : Developer's Link

Sign up to request clarification or add additional context in comments.

Comments

2

The array is your resource, so you access it by doing following

  Resources resources=getResources();
  String[] mCountry=resources.getStringArray(R.array.countries_array);

or just in one line

String[] mCountry=getResources().getStringArray(R.array.countries_array);

For further references See this too

7 Comments

This is duplicate answer
@VicJordan I will let the timestamp help you,I was just typing when the other answer was posted anyway
@VicJordan No, there's no evidence that it's a duplicate answer. In facts, it seems a genuine answer.
@nobalG Der, I was reading this question and during that first answer appeared was from Prag's then this answer.
no offence for his answer.. It happens... Thnk u for your support.. @VicJordan
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.