我通过解析一个 xml 文件遇到了一个小问题,该文件位于我的智能手机的 sdcard 上。
xml结构看起来像:
....
<nd ref="1364745028"/>
<nd ref="1364745137"/>
<nd ref="2121048622"/>
<nd ref="2121048651"/>
<tag v="forest" k="landuse"/>
</way>
-<way id="25089587">
<nd ref="273065498"/>
<nd ref="273065777"/>
<nd ref="1882290182"/>
<nd ref="1882290194"/>
....
我特别想要“森林”的价值。我怎样才能获得价值?我目前的代码如下:
....
try {
File root = android.os.Enviroment.getExternalStorageDirectory();
Document document = builder.parse(new FileInputStream(root.getAbsolutePath() + "/xmls/xapi.xml");
NodeList tag = document.getElementsByTagName("tag");
for(int i = 0; i < tag.getLength(); i++) {
Element element = (Element) tag.item(i);
NodeList value = element.getElementsByTagName("k");
Element line = (Element) value.item(0);
parameter.setText(line.getTextContent());
}
}catch(SAXException e) {
....
}catch(IOException e) {
....
}
当我单击一个按钮时,应用程序崩溃了,其中 onlick-method 是这些 try-catch 块。任何人的想法,如何解决它?提前致谢。
EDIT1 错误信息
02-20 17:54:42.495: E/AndroidRuntime(12627): FATAL EXCEPTION: main
02-20 17:54:42.495: E/AndroidRuntime(12627): java.lang.IllegalStateException: Could not execute method of the activity
02-20 17:54:42.495: E/AndroidRuntime(12627): at android.view.View$1.onClick(View.java:3100)
02-20 17:54:42.495: E/AndroidRuntime(12627): at android.view.View.performClick(View.java:3627)
02-20 17:54:42.495: E/AndroidRuntime(12627): at android.view.View$PerformClick.run(View.java:14329)
02-20 17:54:42.495: E/AndroidRuntime(12627): at android.os.Handler.handleCallback(Handler.java:605)
02-20 17:54:42.495: E/AndroidRuntime(12627): at android.os.Handler.dispatchMessage(Handler.java:92)
02-20 17:54:42.495: E/AndroidRuntime(12627): at android.os.Looper.loop(Looper.java:137)
02-20 17:54:42.495: E/AndroidRuntime(12627): at android.app.ActivityThread.main(ActivityThread.java:4511)
02-20 17:54:42.495: E/AndroidRuntime(12627): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 17:54:42.495: E/AndroidRuntime(12627): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 17:54:42.495: E/AndroidRuntime(12627): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
02-20 17:54:42.495: E/AndroidRuntime(12627): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
02-20 17:54:42.495: E/AndroidRuntime(12627): at dalvik.system.NativeStart.main(Native Method)
02-20 17:54:42.495: E/AndroidRuntime(12627): Caused by: java.lang.reflect.InvocationTargetException
02-20 17:54:42.495: E/AndroidRuntime(12627): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 17:54:42.495: E/AndroidRuntime(12627): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 17:54:42.495: E/AndroidRuntime(12627): at android.view.View$1.onClick(View.java:3095)
02-20 17:54:42.495: E/AndroidRuntime(12627): ... 11 more
02-20 17:54:42.495: E/AndroidRuntime(12627): Caused by: java.lang.NullPointerException
02-20 17:54:42.495: E/AndroidRuntime(12627): at com.example.parsexml.MainActivity.ReadXMLFromSD(MainActivity.java:116)
02-20 17:54:42.495: E/AndroidRuntime(12627): ... 14 more