public InputSource getResource(String path) {
File file = new File(path);
System.out.println("文件名"+file.getAbsolutePath());
System.out.println("进入方法");
try {
System.out.println("---1进入了try");
Reader reader=new FileReader(file);
System.out.println("---2进入了try");
InputSource is=new InputSource(reader);
return is ;
} catch (FileNotFoundException e) {
System.out.println("出错了!");
e.printStackTrace();
}
return null;
}
以上方法是获得一个 InputSource ,经调试 红色部分无法通过 public void onClick(View v) {
SAXParserFactory fac = SAXParserFactory.newInstance();
FileUtils fu = new FileUtils();
String path = "F:\\sample.xml";

InputSource is = fu.getResource(path);

System.out.println(is==null);//控制台输出为true
try {
XMLReader reader = fac.newSAXParser().getXMLReader();
reader.setContentHandler(new MyContentHandler());
reader.parse(is);
} catch (SAXException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
通过鼠标点击事件 开始解析XML 此时 is为null
04-01 07:15:51.556: E/AndroidRuntime(319): Uncaught handler: thread main exiting due to uncaught exception
04-01 07:15:51.587: E/AndroidRuntime(319): java.lang.NullPointerException
04-01 07:15:51.587: E/AndroidRuntime(319):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:270)
04-01 07:15:51.587: E/AndroidRuntime(319):  at com.myandroid.download.xmlParseActivity$btnListener.onClick(xmlParseActivity.java:46)此处为reader.parse(is)04-01 07:40:57.616: I/System.out(416): 文件名/F:/sample.xml
04-01 07:40:57.616: I/System.out(416): 进入方法
04-01 07:40:57.626: I/System.out(416): ---1进入了try
04-01 07:40:57.626: I/System.out(416): 出错了!
04-01 07:40:57.686: I/System.out(416): true