我正在尝试解析 xml 文件并从中提取数据以显示在我的 android 应用程序中。xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<record>
<study id="1">
<topic>SAX Parser</topic>
<content>Learn how to parse XML using SAXParser</content>
<author>Pete Houston</author>
<date>02-Oct-2011</date>
</study>
该文件存储在 res/raw 文件夹中。android 活动调用一个函数,该函数试图读取 xml 文件并对其进行解析以检索数据,然后将其存储到 Study 类型的对象(由我定义)中。然后该对象由函数返回。
我的代码的骨架结构如下:
public class XmlParserActivity extends Activity {
Study study=null;
protected void onCreate(Bundle savedInstanceState) {
//...android code and some variable initializations
try{
study = parseXml(getResources().openRawResource(R.raw.record));
}catch(Exception e){
e.printStackTrace();
finish();
}
}
public Study parseXml(InputStream is){
//code for parsing the input stream
return study;
}
当我在模拟器中启动应用程序时,它会崩溃并产生 android.content.res.ResourcesNotFoundException。我访问 xml 文件的方式有问题吗?还是我需要在 AndroidManifest.xml 文件中设置一些权限?我已经搜索了这个站点和其他站点,但没有找到一个令人满意的解决我的问题的方法。任何指导将不胜感激。
日志信息:
01-09 15:49:55.801: I/Process(1152): Sending signal. PID: 1152 SIG: 9
01-09 15:50:04.174: E/Trace(1198): error opening trace file: No such file or directory(2)
01-09 15:50:04.992: W/ResourceType(1198): No package identifier when getting value for resource number 0x00000001
01-09 15:50:05.002: D/AndroidRuntime(1198): Shutting down VM
01-09 15:50:05.002: W/dalvikvm(1198): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
01-09 15:50:05.012: E/AndroidRuntime(1198): FATAL EXCEPTION: main
01-09 15:50:05.012: E/AndroidRuntime(1198): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidxmlparser/com.example.androidxmlparser.XmlParserActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.os.Handler.dispatchMessage(Handler.java:99)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.os.Looper.loop(Looper.java:137)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-09 15:50:05.012: E/AndroidRuntime(1198): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:50:05.012: E/AndroidRuntime(1198): at java.lang.reflect.Method.invoke(Method.java:511)
01-09 15:50:05.012: E/AndroidRuntime(1198): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-09 15:50:05.012: E/AndroidRuntime(1198): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-09 15:50:05.012: E/AndroidRuntime(1198): at dalvik.system.NativeStart.main(Native Method)
01-09 15:50:05.012: E/AndroidRuntime(1198): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.content.res.Resources.getText(Resources.java:229)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.widget.TextView.setText(TextView.java:3620)
01-09 15:50:05.012: E/AndroidRuntime(1198): at com.example.androidxmlparser.XmlParserActivity.onCreate(XmlParserActivity.java:34)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.Activity.performCreate(Activity.java:5008)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-09 15:50:05.012: E/AndroidRuntime(1198): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-09 15:50:05.012: E/AndroidRuntime(1198): ... 11 more