android中的XML解析中的FileNotFoundException

时间:2011-12-08 10:55:07

标签: android xml-parsing filenotfoundexception

我在android中开发一个应用程序我正在解析一个存储在res / raw文件夹中的xml但我得到一个错误FileNotFound 继承我的代码

            Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setCoalescing(true);
        InputStream is = this.getResources().openRawResource(R.raw.earth); 
        try{

            DocumentBuilder db = dbf.newDocumentBuilder();
            doc = db.parse(is,null);

        }catch(ParserConfigurationException e){
            Log.d("XML parse Error:",e.getMessage());
            return;
        }catch(SAXException e){
            Log.d("Wrong XML File Structure",e.getMessage());
            return;
        }catch(IOException e){
            Log.d("IOException", e.getMessage());
            return;
        }

Log cat

 12-08 14:37:17.893: E/AndroidRuntime(502): Uncaught handler: thread main exiting due to uncaught exception
12-08 14:37:17.923: E/AndroidRuntime(502): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.xml/org.example.xml.XMLparseActivity}: android.content.res.Resources$NotFoundException: File Hello World, XMLparseActivity! from drawable resource ID #0x7f040000
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.os.Looper.loop(Looper.java:123)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.app.ActivityThread.main(ActivityThread.java:4363)
12-08 14:37:17.923: E/AndroidRuntime(502):  at java.lang.reflect.Method.invokeNative(Native Method)
12-08 14:37:17.923: E/AndroidRuntime(502):  at java.lang.reflect.Method.invoke(Method.java:521)
12-08 14:37:17.923: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-08 14:37:17.923: E/AndroidRuntime(502):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-08 14:37:17.923: E/AndroidRuntime(502):  at dalvik.system.NativeStart.main(Native Method)
12-08 14:37:17.923: E/AndroidRuntime(502): Caused by: android.content.res.Resources$NotFoundException: File Hello World, XMLparseActivity! from drawable resource ID #0x7f040000
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.content.res.Resources.openRawResource(Resources.java:822)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.content.res.Resources.openRawResource(Resources.java:798)
12-08 14:37:17.923: E/AndroidRuntime(502):  at org.example.xml.XMLparseActivity.onCreate(XMLparseActivity.java:47)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
12-08 14:37:17.923: E/AndroidRuntime(502):  ... 11 more
12-08 14:37:17.923: E/AndroidRuntime(502): Caused by: java.io.FileNotFoundException: Hello World, XMLparseActivity!
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.content.res.AssetManager.openNonAssetNative(Native Method)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.content.res.AssetManager.openNonAsset(AssetManager.java:390)
12-08 14:37:17.923: E/AndroidRuntime(502):  at android.content.res.Resources.openRawResource(Resources.java:819)
12-08 14:37:17.923: E/AndroidRuntime(502):  ... 15 more

请回复是否有任何线索

2 个答案:

答案 0 :(得分:0)

你是如何构建你的apk的?如果您使用m2eclipse和maven,则最近版本中存在一个错误,导致无法复制原始资源。您需要使用命令行中的mvn创建apk。

答案 1 :(得分:0)

错误必须是因为这一行

文件库存=新文件(“src / Stocks.xml”);

在我的代码中我将xml文件放在eclipse的src文件夹下。现在尝试使用您的代码,它将起作用。