简体   繁体   中英

Android load file from classpath causing crash

I'm trying to load a file from my classpath in a static context in Android, and every similar question on SO suggests using MyClass.class.getClassLoader().getResourcesAsStream(<filepath>) , but this causes my app to crash before it opens.

My target SDK is 19, min SDK level is 17 and I'm using a phone running Android Lollipop

This is the part of code where I'm trying to load the file "locations.xml":

public static final String LOCATIONS_FILE_PATH = "locations.xml";

public static ArrayList<City> getLocations(String locations_file_path) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = null;
    Document document = null;
    try {
        builder = factory.newDocumentBuilder();
        document = builder.parse(
        City.class.getClassLoader().getResourceAsStream(locations_file_path));

The file is located in the same package as the java classes that is referencing it.

The error given in logcat is an IllegalArgumentException in DocumentBuilder.parse(...) because City.class.getClassLoader().getResourceAsStream("locations.xml")) returns null .

I think that you'll want to verify that in the final apk file, the xml file is actually included where you think it is.

The more common pattern for Android is to put the file in the 'assets' directory, and then load it from there using the Activity's getAssets() method.

See Read Assets file as string

As an alternative to getResourceAsStream you could use FileInputStream as explained in this tutorial

Please note that If FileInputStream also return null , then it's a big chance that as @GreyBeardedGeek said, the xml file is actually not included where you expect it to in the final apk file.

Relevant code:

import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class DocumentBuilderDemo {

   public static void main(String[] args) {

      // create a new DocumentBuilderFactory
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      try {
         // use the factory to create a documentbuilder
         DocumentBuilder builder = factory.newDocumentBuilder();

         // create a new document from input stream
         FileInputStream fis = new FileInputStream("Student.xml");
         Document doc = builder.parse(fis);

         // get the first element
         Element element = doc.getDocumentElement();

         // get all child nodes
         NodeList nodes = element.getChildNodes();

         // print the text content of each child
         for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println("" + nodes.item(i).getTextContent());
         }
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Student.xml (In your case, locations.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student id="10">
   <age>12</age>
   <name>Malik</name>
</student>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM