从Asset NullPointerException获取XML

时间:2012-09-19 17:43:22

标签: android xml

我正在处理this示例,其中作者通过HTTP请求拉取+解析xml文件。我试图操纵这个项目,而是从assets文件夹加载相同的xml。当我加载它但是我得到一个空指针。我正在传递上下文,因为我认为除了从assets文件夹加载“url”之外我还应该这样做。我在这段代码中的错误在哪里?

MainActivity snippet-initializing url string并调用getXML

static final String URL = "file:///android_asset/sample.xml";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ...
    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL, context); // getting XML
    ...
}

XMLParser Class'方法:

public String getXmlFromUrl(String URL, Context context){
    String xml = null;
    AssetManager am = context.getAssets();
    try {
        InputStream is = am.open(URL); //This is line 40
        int length = is.available();
        byte[] data = new byte[length];
        is.read(data);
        xml = new String(data);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    return xml;
}

编辑:添加了Logcat

09-19 13:41:43.442: W/dalvikvm(4841): threadid=1: thread exiting with uncaught exception (group=0x40015560)
09-19 13:41:43.442: E/AndroidRuntime(4841): FATAL EXCEPTION: main
09-19 13:41:43.442: E/AndroidRuntime(4841): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.AndroidXMLParsingActivity}: java.lang.NullPointerException
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.os.Looper.loop(Looper.java:130)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.main(ActivityThread.java:3683)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at java.lang.reflect.Method.invokeNative(Native Method)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at java.lang.reflect.Method.invoke(Method.java:507)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at dalvik.system.NativeStart.main(Native Method)
09-19 13:41:43.442: E/AndroidRuntime(4841): Caused by: java.lang.NullPointerException
09-19 13:41:43.442: E/AndroidRuntime(4841):     at com.androidhive.xmlparsing.XMLParser.getXmlFromUrl(XMLParser.java:40)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:42)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-19 13:41:43.442: E/AndroidRuntime(4841):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
09-19 13:41:43.442: E/AndroidRuntime(4841):     ... 11 more

1 个答案:

答案 0 :(得分:0)

您在哪里定义了contextString xml = parser.getXmlFromUrl(URL, context); // getting XML变量的值?

从我看到的参数是 null 。在getXmlFromUrl()方法内,您不需要提供Context作为参数,因为您位于Activity内。只需在没有getAssets()变量之前调用context方法。