0

first of all excuse for my english. My problem is on title. Here my code

ArrayList<String> almazaras=null;
almazaras=new ArrayList<>(R.array.almazaras);

It crash on asignation. And here is my XML:

<resources>
<string-array name="almazaras">
    <item>SAN SEBASTIAN DE ALFACAR</item>
    <item>ACEITES ALGARINEJO</item>
    <item>AGRICOLA LOS TAJOS</item>
    <item>AGRICOLA SANTA BARBARA DE BAZA</item>
    <item>SAN SEBASTIAN DE BENALUA</item>
    <item>SANTA ISABEL DE CAMPOTEJAS</item>
    <item>NTRA. SRA. DE LOS REMEDIOS DE CAMPOTEJAS</item>
    <item>NTRA. SRA. DEL ROSARIO CASTRIL</item>
    <item>SAN ANTONIO DE COGOLLOS</item>
    <item>NTRA. SRA. DEL PILAR DE COLOMERA</item>
    <item>LA ESPERANZA DEL CAMPO</item>
    <item>NTRA. SRA. DE LA CABEZA DE CULLAR</item>
    <item>NTRA. SRA. DEL ROSARIO DE DEHESAS</item>
    <item>NTRA. SRA. DE LOS DOLORES</item>
    <item>SAN ISIDRO DE DEIFONTES</item>
    <item>S.A.T. NTRA. SRA. DEL PERPETUO SOCORRO</item>
    <item>BARAILA DE DOMINGO PEREZ</item>
    <item>NTRA. SRA. DE LOS DOLORES DE FREILA</item>
    <item>ACEITES FUENTES DE CESNA</item>
    <item>SAN FRANCISCO SERRANO</item>
    <item>AGRO-OLIV NTRA. SRA. SOLEDAD DE HUESCAR</item>
    <item>AGRICOLA SAN ROGELIO DE ILLORA</item>
    <item>NTRA. SRA. DE LOS REMEDIOS DE IZNALLOZ</item>
    <item>OLIJAYENA</item>
    <item>UNION AGRICOLA SAN JOSE</item>
    <item>SAN ISIDRO DE LOJA</item>
    <item>SAN FRANCISCO DE ASIS</item>
    <item>ALMAZARA NTRA. SRA. DE LOS REMEDIOS</item>
    <item>VIRGEN DE LA CABEZA</item>
    <item>ALMAZARA DE MONTILLANA</item>
    <item>SAN ILDEFONSO DE PELIGROS</item>
    <item>SAN ROQUE DE PINOS DEL VALLE</item>
    <item>SANTA MONICA DE PIÑAR</item>
    <item>PUERTO LOPE</item>
    <item>SANTA ANA DE SALAR</item>
    <item>ACEITES EL CORTIJILLO</item>
    <item>OLEOTROPIC</item>
    <item>TEMPLE OLIVA</item>
    <item>CAMPO-AGRO OLIVARERA</item>
    <item>AGRARIA CERRO GORDO</item>
    <item>SAN LORENZO DE ZAGRA</item>
    <item>NTRA. SRA. DE LA CABEZA DE ZUJAR</item>
</string-array>

And here my error:

04-15 12:44:48.406    4631-4631/com.example.soke.faeca E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.OutOfMemoryError: java.lang.Object[] of length 2131099649 exceeds the VM limit
        at java.util.ArrayList.<init>(ArrayList.java:75)
        at com.example.soke.faeca.enviarGrupo.onCreate(enviarGrupo.java:55)
        at android.app.Activity.performCreate(Activity.java:5047)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2056)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117)
        at android.app.ActivityThread.access$700(ActivityThread.java:134)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
        at dalvik.system.NativeStart.main(Native Method)

I don't know why. Doing this on different files and same way (getting resources from an XML) it works clean. I spent a lot searching on Google and this web without results. I hope you can solve it! Thanks for any help!

2
  • Show the code. Here you try and allocate an array of Object with more than 2^31 - 1 elements. Commented Apr 15, 2015 at 11:11
  • use String[]=resources.getStringArray(R.array.plants); and then create arraylist using this string array Commented Apr 15, 2015 at 11:21

5 Answers 5

1

R.array.almazaras is an integer constant (in your example, it's 2131099649).

You should be using the constant to fetch the resource first, then put them in your ArrayList.

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

Comments

0

First get String array by,

Resources res = getResources();
String[] almazarasItems  = res.getStringArray(R.array.almazaras);

And add the String array to ArrayList by,

List<String[]> arrayList = new ArrayList<String[]>();
arrayList.add(almazarasItems);

Comments

0

@Kayaman is right. You're referring to the constant reference to the array.

Try this, it should do the trick.

    String arr[] = getResources().getStringArray(R.array.almazaras);
    ArrayList<String> almazaras= (ArrayList)Arrays.asList(arr);

Comments

0

You can't call almazaras=new ArrayList<>(R.array.almazaras); to initialize your List. In Android, R.array.almazaras is the address of your array resources, like integer-array, string-array. You can find its value which is in Hex format in gen/pkg-name/R. Maybe you can call almazaras=new ArrayList<>(); or almazaras=new ArrayList<>(a-int-value);.

Comments

0

You are using an ID to initialize an Arraylist. But the constructor oft ArrayList interpreters the Parameter as the length that the ArrayList should be at initialition.

So you are creating an Arraylist with the initial length of 2131099649 which is too much.

You can optain your array with context.getResources().getStringArray(R.array.almazaras);

Comments

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.