0

所以我认为我在以前的线程中质疑错误的方式。我最好这样说。我正在尝试解析 XML 数据:

<playlist>
<title>![CDATA[New Playlist]]</title>
<items>
<item>
<title>HAMD(LA ILAHA ILLALLAH)</title>
<description>Recited By :Alhaaj Muhammad Owais Raza Qadri -- Written By: Mufti-e-Azam Hind Molana Mustafa Raza Khan Noori</description>
<image>http://xxx.xxx.xxx.xxx/~kanz/video/Images/9.jpg</image>
<startFromThis>true</startFromThis>
<duration>510</duration>
<source>http://xxx.xxx.xxx.xxx/~kanz/video/flv/9.flv</source>
<sourceAlt>http://xxx.xxx.xxx.xxx/~kanz/video/mp4/9.mp4</sourceAlt>
<sourceType>direct</sourceType>
</item>
</items>
</playlist>

在我的活动中,我所做的是这样,但 logcat 给出了空指针异常。这是我的活动,下面是 logcat 错误。

static final String URL = "http://xxx.xxx.xxx.xxx/~kanz/video/XML/9.xml";

// XML node keyse.
static final String KEY_SONG = "item"; // parent node
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "description";
static final String KEY_THUMB_URL = "image";
static final String KEY_DURATION = "duration";

NodeList nl = doc.getElementsByTagName(KEY_SONG);

// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
    // creating new HashMap
    HashMap<String, String> map = new HashMap <String, String>();
    Element e = (Element) nl.item(i);
    // adding each child node to HashMap key => value
    map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
    map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
    map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
    map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));

    // adding HashList to ArrayList
    songsList.add(map);
}

Logcat 给出 Unexpected Token 错误并返回 null 导致空指针异常。

03-28 14:31:34.059: E/Error:(895): Unexpected token (position:TEXT @1:4 in java.io.StringReader@40d220e0) 
03-28 14:31:34.080: I/System.out(895): null
03-28 14:42:59.560: D/AndroidRuntime(978): Shutting down VM
03-28 14:42:59.560: W/dalvikvm(978): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-28 14:42:59.639: E/AndroidRuntime(978): FATAL EXCEPTION: main
03-28 14:42:59.639: E/AndroidRuntime(978): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.razatubevidurl/com.example.razatubevidurl.CustomizedListView}: java.lang.NullPointerException
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.os.Looper.loop(Looper.java:137)
03-28 14:42:59.639: E/AndroidRuntime(978):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-28 14:42:59.639: E/AndroidRuntime(978):  at java.lang.reflect.Method.invokeNative(Native Method)
03-28 14:42:59.639: E/AndroidRuntime(978):  at java.lang.reflect.Method.invoke(Method.java:511)
03-28 14:42:59.639: E/AndroidRuntime(978):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-28 14:42:59.639: E/AndroidRuntime(978):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-28 14:42:59.639: E/AndroidRuntime(978):  at dalvik.system.NativeStart.main(Native Method)
03-28 14:42:59.639: E/AndroidRuntime(978): Caused by: java.lang.NullPointerException

请告诉我我做错了什么。我认为我之前的问题含糊不清,在这个问题中,我尽力使问题清晰易懂。

4

1 回答 1

1

我想这与编码有关。也许您的解析器不喜欢感叹号 (!)。您能否确保使用正确编码的 XML 文件?(UTF-8)

否则,你会遇到这个问题:KXmlParser 在开始 RSS paing 时抛出“Unexpected token”异常

我用谷歌搜索了一下,看起来最简单的修复方法就在这里。您可以尝试添加此方法。(根据您使用的教程,将其放在 ImageLoader 类中。)

import java.io.PushbackInputStream;
private InputStream checkForUtf8BOMAndDiscardIfAny(InputStream inputStream) throws IOException {
PushbackInputStream pushbackInputStream = new PushbackInputStream(new BufferedInputStream(inputStream), 3);
byte[] bom = new byte[3];
if (pushbackInputStream.read(bom) != -1) {
    if (!(bom[0] == (byte) 0xEF && bom[1] == (byte) 0xBB && bom[2] == (byte) 0xBF)) {
        pushbackInputStream.unread(bom);
    }
}
return pushbackInputStream; }

然后你可以做

InputStream is=conn.getInputStream();
is = checkForUtf8BOMAndDiscardIfAny(is);

我没有机会测试该方法,但它应该这样做。另外,请参阅字节顺序标记搞砸了 Java 中的文件读取

于 2013-03-28T16:11:19.200 回答