Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I be using something other than getResource().getStringArray() to populate a large array?

Tags:

android

Following the Android sample for populating a ListView, I query an array from my strings.xml using Activity.getResource().getStringArray():

String [] mDefinitions = getResources().getStringArray(R.array.definition_array);

The documentation for this method is pretty clear and I didn't expect to encounter any problems with it:

Return the string array associated with a particular resource ID.

This approach worked as expected for small data sets. However, when I populated strings.xml with the full data set (close to 2000 entries), I find that the app crashed when it tried to load the resource. I noticed this error in the console log:

ReferenceTable overflow (max=512)

Playing around with the number of items in my string-array I confirmed that the error was reproducible when the number of items exceeded ~700 entries.

Googling the problem has turned up some examples of other developers having the same problem, but I can't find anything in the Android documentation to explain it.

Someone has gone to the trouble of creating an issue for the problem on the Android Google Code page but neither it, or any of the posts I came across, received a satisfactory answer.

Am I approaching the problem the wrong way? Should I be populating the data myself (loading a file and parsing JSON or similar) and avoid the issue entirely? It feels like I am missing something obvious here.

like image 374
RedBlueThing Avatar asked Aug 19 '10 13:08

RedBlueThing


2 Answers

I need to be able to parse large XML files for my particular application (I already had data encoded that way and I wanted to keep it consistant).

So my first attempt was to use getStringArray, which suffers from the problem described in the question:

String [] mDefinitions = getResources().getStringArray(R.array.definition_array);

My second attempt suffers from the same limitation that I encounted with getStringArray. As soon as I tried processing a larger XML file (> 500K), I got a DalvikVM crash on getXml:

 XmlResourceParser parser = getResources().getXml(R.xml.index);

 try {
     int eventType = parser.getEventType();

     while (eventType != XmlPullParser.END_DOCUMENT) {
         String name = null;

         switch (eventType){
             case XmlPullParser.START_TAG:
                 // handle open tags
                 break;
             case XmlPullParser.END_TAG:
                 // handle close tags
                 break;
         }

         eventType = parser.next();
     }
 }
 catch (XmlPullParserException e) {
     throw new RuntimeException("Cannot parse XML");
 }
 catch (IOException e) {
     throw new RuntimeException("Cannot parse XML");
 }
 finally {
     parser.close();
 }

My final solution, which uses the SAX parser on a InputStream generated from the raw resource works. I can parse large XML files without the DalvikVM crashing:

 InputStream is = getResources().openRawResource(R.raw.index);
 XmlHandler myXMLHandler = new XmlHandler();

 SAXParserFactory spf = SAXParserFactory.newInstance();
 SAXParser sp = spf.newSAXParser();
 XMLReader xr = sp.getXMLReader();

 xr.setContentHandler(myXMLHandler);
 xr.parse(new InputSource (is));

 } catch (Exception e) {
     System.out.println("XML Pasing Excpetion = " + e);
 }

Where XmlHandler is:

 public class XmlHandler extends DefaultHandler {

     @Override
     public void startElement(String uri, String localName, String qName,
     Attributes attributes) throws SAXException {
     // handle elements open
     }

     @Override
     public void endElement(String uri, String localName, String qName)
     throws SAXException {  
     // handle element close
     }

     @Override
     public void characters(char[] ch, int start, int length)
     throws SAXException {
     // handle tag characters <blah>stuff</blah>
     }

 }
like image 161
RedBlueThing Avatar answered Sep 19 '22 19:09

RedBlueThing


Not sure whats the objective of your application but have you thought about using a csv file.

 InputStream is = c.getResources().openRawResource(R.raw.csv_file);
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String readLine = null;

        int column = 1; 

        try {
            while ((readLine = br.readLine()) != null) {


            }
        } catch (IOException e) {
            e.printStackTrace();
        }
like image 45
Sameer Segal Avatar answered Sep 21 '22 19:09

Sameer Segal