如何将R资源数组设置为字符串数组
我试试这个,但我得到一个错误:
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)
这是我的代码
String[] mCountry = new String[R.array.countries_array];
XML中的
<string-array name="countries_array">
<item>Afghanistan</item>
<item>Albania</item>
<item>Algeria</item>
<item>American Samoa</item>
<item>Andorra</item>
...
答案 0 :(得分:3)
尝试使用getStringArray()
函数获取字符串数组..
代码:
String[] mCountry;
mCountry= getResources().getStringArray(R.array.countries_array);
参考文献:
public String[] getStringArray (int id)
Return the string array associated with a particular resource ID.
请参阅此内容以获取更多信息:Developer's Link
答案 1 :(得分:2)
数组是您的资源,因此您可以通过执行以下操作来访问它
Resources resources=getResources();
String[] mCountry=resources.getStringArray(R.array.countries_array);
或只是一行
String[] mCountry=getResources().getStringArray(R.array.countries_array);
有关详细参考See this too