本帖最后由 Robot221 于 2011-11-07 14:42:34 编辑

解决方案 »

  1.   


    EmpPartBean文件代码如下:
    package com.dai.listview6;public class EmpPartBean { private int id;
    private String name;
    private String employeeNum;
    private String department;
    private String phone;

    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getEmployeeNum() {
    return employeeNum;
    }
    public void setEmployeeNum(String employeeNum) {
    this.employeeNum = employeeNum;
    }
    public String getDepartment() {
    return department;
    }
    public void setDepartment(String department) {
    this.department = department;
    }
    public String getPhone() {
    return phone;
    }
    public void setPhone(String phone) {
    this.phone = phone;
    }

    }
      

  2.   


    employees.xml文件代码如下:
    <?xml version="1.0" encoding="utf-8"?>
    <employees>
        <employee id="1" >       
    <name>张天瑞</name>
    <sex>男</sex>
    <employeeNum>A12345</employeeNum>
    <department>人力资源部</department>
    <phone>13612345678</phone>
    <email>[email protected]</email>
    <address>湖东路</address>
    <>你好,大家好!</>        
        </employee>
        <employee id="2">        
    <name>张康丹</name>
    <sex>男</sex>
    <employeeNum>A12346</employeeNum>
    <department>人力资源部</department>
    <phone>13612345688</phone>
    <email>[email protected]</email>
    <address>福马路</address>
    <>你好,大家好!2</>        
        </employee>
    </employees>
      

  3.   

    运行该应用程序,报出以下异常:
    11-07 14:53:48.073: W/System.err(1098): java.io.FileNotFoundException: /xml/employees.xml
    11-07 14:53:48.085: W/System.err(1098):  at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
    11-07 14:53:48.093: W/System.err(1098):  at java.io.FileInputStream.<init>(FileInputStream.java:77)
    11-07 14:53:48.093: W/System.err(1098):  at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:138)
    11-07 14:53:48.093: W/System.err(1098):  at com.dai.listview6.XMLHelper.getAllPart(XMLHelper.java:117)
    11-07 14:53:48.093: W/System.err(1098):  at com.dai.listview6.ListView6Activity.onCreate(ListView6Activity.java:37)
    11-07 14:53:48.093: W/System.err(1098):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    11-07 14:53:48.093: W/System.err(1098):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    11-07 14:53:48.093: W/System.err(1098):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    11-07 14:53:48.123: W/System.err(1098):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    11-07 14:53:48.123: W/System.err(1098):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    11-07 14:53:48.123: W/System.err(1098):  at android.os.Handler.dispatchMessage(Handler.java:99)
    11-07 14:53:48.123: W/System.err(1098):  at android.os.Looper.loop(Looper.java:123)
    11-07 14:53:48.123: W/System.err(1098):  at android.app.ActivityThread.main(ActivityThread.java:4363)
    11-07 14:53:48.133: W/System.err(1098):  at java.lang.reflect.Method.invokeNative(Native Method)
    11-07 14:53:48.133: W/System.err(1098):  at java.lang.reflect.Method.invoke(Method.java:521)
    11-07 14:53:48.133: W/System.err(1098):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    11-07 14:53:48.133: W/System.err(1098):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    11-07 14:53:48.133: W/System.err(1098):  at dalvik.system.NativeStart.main(Native Method)
    请问各位大侠,问题出在何处?是employees.xml文件的存放路径有误,还是什么问题
    到底怎样做才对啊?
      

  4.   

    你把文件放在src下面或者assert下都可以
    把你的方法不要写成静态的就行啊
    InputStream is=this.getClass().getClassLoader().getResourceAsStream("employees.xml");  
    InputStream is=this.Context.getResources().getAssets().open("employees.xml");
              
                
      

  5.   

    getContext().getResources().getXml("")这种方法没有找到转成流的
      

  6.   

    使用InputStream is=this.Context.getResources().getAssets().open("employees.xml");
    会显示Context cannot be resolved or is not a field
      

  7.   

    使用InputStream is=this.Context.getResources().getAssets().open("employees.xml");
    运行后出现如下这样的异常
    11-07 15:52:21.743: D/AndroidRuntime(1218): Shutting down VM
    11-07 15:52:21.753: W/dalvikvm(1218): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
    11-07 15:52:21.753: E/AndroidRuntime(1218): Uncaught handler: thread main exiting due to uncaught exception
    11-07 15:52:21.773: E/AndroidRuntime(1218): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dai.listview6/com.dai.listview6.ListView6Activity}: java.lang.IllegalArgumentException
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.os.Handler.dispatchMessage(Handler.java:99)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.os.Looper.loop(Looper.java:123)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.app.ActivityThread.main(ActivityThread.java:4363)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at java.lang.reflect.Method.invokeNative(Native Method)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at java.lang.reflect.Method.invoke(Method.java:521)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at dalvik.system.NativeStart.main(Native Method)
    11-07 15:52:21.773: E/AndroidRuntime(1218): Caused by: java.lang.IllegalArgumentException
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:154)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at com.dai.listview6.XMLHelper.getAllPart(XMLHelper.java:119)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at com.dai.listview6.ListView6Activity.onCreate(ListView6Activity.java:37)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
    11-07 15:52:21.773: E/AndroidRuntime(1218):  ... 11 more
    11-07 15:52:21.806: I/dalvikvm(1218): threadid=7: reacting to signal 3
    11-07 15:52:21.806: E/dalvikvm(1218): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
      

  8.   

    使用InputStream is=this.Context.getResources().getAssets().open("employees.xml");
    然后将employees.xml文件放在src目录下就可以了
    真的非常感谢!