2

In an app I'm writing, I'm loading an XML into a Java String[], then turning each item into an object and adding these to an array of objects.

Below is my code:

The Java part, with a do-while loop

ArrayList<ExpandListGroup> list = new ArrayList<ExpandListGroup>();
    ArrayList<ExpandListChild> list2 = new ArrayList<ExpandListChild>();
    ExpandListGroup gru1 = new ExpandListGroup();
    gru1.setName(getString(R.string.album_vancouver));
    String[] tempArray = getResources().getStringArray(R.array.album_array_vancouver);
    int count = 0;
    int max = tempArray.length;
    do {
        ExpandListChild ch = new ExpandListChild();
        ch.setName(tempArray[count]);
        ch.setTag(null);
        list2.add(ch);
    } while(count <= max);
    gru1.setItems(list2);
    list.add(gru1);

And the arrays in XML

<string name="album_vancouver">Vancouver</string>
<string name="song_vancouver_1">Future Wars</string>
<string name="song_vancouver_2">A Word Of Welcome And Of Warning</string>
<string name="song_vancouver_3">See You In Vancouver</string>
<string name="song_vancouver_4">To Withstand The Force Of Storms</string>
<string name="song_vancouver_5">He Is Here, He Is Not Afraid</string>
<string name="song_vancouver_6">The Surgeon And The Scientist</string>
<string name="song_vancouver_7">Fairmount</string>
<string name="song_vancouver_8">Untitled</string>
<string-array name="album_array_vancouver">
    <item>@string/song_vancouver_1</item>
    <item>@string/song_vancouver_2</item>
    <item>@string/song_vancouver_3</item>
    <item>@string/song_vancouver_4</item>
    <item>@string/song_vancouver_5</item>
    <item>@string/song_vancouver_6</item>
    <item>@string/song_vancouver_7</item>
    <item>@string/song_vancouver_8</item>
</string-array>

I get a java.lang.OutOfMemoryError at runtime, at the line "list2.add(ch)".
What can I do to make this work?

0

1 Answer 1

2

You have infinite loop there, you are not incrementing the count variable inside the do-while statement.

Use ch.setName(tempArray[count++]);

Also, the while condition should be count < max, otherwise you will get ArrayIndexOutOfBoundsException.

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

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.