Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中XML解析中的FileNotFoundException_Android_Xml Parsing_Filenotfoundexception - Fatal编程技术网

android中XML解析中的FileNotFoundException

android中XML解析中的FileNotFoundException,android,xml-parsing,filenotfoundexception,Android,Xml Parsing,Filenotfoundexception,我正在android中开发一个应用程序,我正在解析一个存储在res/raw文件夹中的xml,但我收到一个错误文件NotFound 这是我的密码 Document doc = null; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setCoalescing(true); InputStream is = this.getReso

我正在android中开发一个应用程序,我正在解析一个存储在res/raw文件夹中的xml,但我收到一个错误文件NotFound 这是我的密码

            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;
        }
原木猫是

 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

如果有任何线索,请回复

您是如何构建apk的?如果您使用的是m2eclipse和maven,那么在最新版本中有一个bug,它阻止了原始资源的复制。您需要从命令行使用mvn创建apk

错误一定是因为这一行

文件stocks=新文件(“src/stocks.xml”)


在我的代码中,我将xml文件放在eclipse的src文件夹下。现在试试你的代码,它会工作。

如果你确定文件在原始文件夹中,请先清理后构建是的,我清理后构建,但错误相同确保你的文件在res/raw/your_文件中。是的,它在那里res/raw/book.xml如果它是名为book.xml的文件,你必须更改这行代码InputStream=this.getResources().openrawsource(R.raw.earth);要使用R.raw.bookmvn android:apk-这里有完整的文档:谢谢你的回答,我得到了我的问题的解决方案如果你想将xml文件保存在raw文件夹中,请始终另存为.txt而不是.xml查看此链接非常好地解释xml解析