0

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

MainActivity 片段初始化 url 字符串并调用 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 类的方法:

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
4

1 回答 1

0

你在哪里定义了一个context变量的值String xml = parser.getXmlFromUrl(URL, context); // getting XML

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

于 2012-09-19T18:02:49.903 回答