【问题标题】:Android load file from classpath causing crashAndroid从类路径加载文件导致崩溃
【发布时间】:2015-03-11 20:53:45
【问题描述】:

我正在尝试在 Android 的静态上下文中从我的类路径加载文件,并且关于 SO 的每个类似问题都建议使用 MyClass.class.getClassLoader().getResourcesAsStream(<filepath>),但这会导致我的应用在打开之前崩溃。

我的目标 SDK 是 19,最低 SDK 级别是 17,我使用的是运行 Android Lollipop 的手机

这是我试图加载文件“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));

该文件与引用它的 java 类位于同一个包中。

logcat 中给出的错误是DocumentBuilder.parse(...) 中的IllegalArgumentException,因为City.class.getClassLoader().getResourceAsStream("locations.xml")) 返回null

【问题讨论】:

  • 试试City.class.getResourceAsStream(locations_file_path));
  • @Marcus 同样的问题
  • 嗯.. 我看不出您的代码有任何问题。您是否尝试过清理项目?
  • 根据thisthis的回答,你的getResourceAsStream相关代码是正确的
  • 阅读它们,但 logcat 确实指出这行代码会导致错误。另外,清理了项目@Marcus

标签: java android xml


【解决方案1】:

我认为您需要验证最终的 apk 文件中,xml 文件实际上包含在您认为的位置。

Android 更常见的模式是将文件放在“assets”目录中,然后使用 Activity 的 getAssets() 方法从那里加载。

Read Assets file as string

【讨论】:

  • 无法让它以其他方式工作。最后,我将文件放在我的资产文件夹中并使用了这一行:InputStream xml = getAssets().open("locations.xml");,它起作用了。
【解决方案2】:

作为getResourceAsStream 的替代品,您可以使用FileInputStream,如this tutorial 中所述

请注意,如果FileInputStream 也返回null,那么很有可能正如@GreyBeardedGeek 所说,xml 文件实际上并未包含在您期望的最终apk 中文件。

相关代码:

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(在你的情况下是locations.xml)

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

【讨论】:

  • 我确定我的解析器可以工作,但传递给解析器的对象为空,因此可能是找不到文件的情况,正如@GreyBeardedGeek 提到的那样。我的问题是正确定位文件。
  • 我会试试这个,如果FileInputStream 也返回null,那么你肯定是xml文件的位置是问题所在。如果是这样,则将文件放在assets 文件夹中,然后从资产中加载它。 @Ozzy
猜你喜欢
  • 1970-01-01
  • 2015-11-17
  • 1970-01-01
  • 2017-12-21
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多