我在以下代码中遇到ClassCastException错误:
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(url);
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_NAME);
// looping through all item nodes <item>
noOfResults = nl.getLength();
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
for (int i = 0; i < nl.getLength(); i++) {
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_PHONE,parser.getValue(e, KEY_PHONE));
map.put(KEY_STATUS,parser.getValue(e, KEY_STATUS));
map.put(KEY_RLINK,parser.getValue(e, KEY_RLINK));
map.put(KEY_RATING,parser.getValue(e, KEY_RATING));
// adding HashList to ArrayList
beerItems.add(map);
}
错误发生在
上 map.put(KEY_ID, parser.getValue(e, KEY_ID));
我已尝试在以下if block
中包含该代码 if (parser.getValue(e, KEY_ID).toString() != null){}
但是我在map.put行的if行上得到了同样的错误。我错过了哪一块拼图?
这是LogCat输出
04-13 13:08:42.626: E/AndroidRuntime(7997): FATAL EXCEPTION: main
04-13 13:08:42.626: E/AndroidRuntime(7997): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.myname.myapp/app.myname.myapp.myappActivity}: java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1821)
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1842)
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.os.Looper.loop(Looper.java:150)
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.app.ActivityThread.main(ActivityThread.java:4263)
04-13 13:08:42.626: E/AndroidRuntime(7997): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 13:08:42.626: E/AndroidRuntime(7997): at java.lang.reflect.Method.invoke(Method.java:507)
04-13 13:08:42.626: E/AndroidRuntime(7997): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 13:08:42.626: E/AndroidRuntime(7997): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 13:08:42.626: E/AndroidRuntime(7997): at dalvik.system.NativeStart.main(Native Method)
04-13 13:08:42.626: E/AndroidRuntime(7997): Caused by: java.lang.ClassCastException: org.apache.harmony.xml.dom.ElementImpl
04-13 13:08:42.626: E/AndroidRuntime(7997): at app.myname.myapp.XMLParser.getValue(XMLParser.java:75)
04-13 13:08:42.626: E/AndroidRuntime(7997): at app.myname.myapp.myappActivity.onCreate(myappActivity.java:103)
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-13 13:08:42.626: E/AndroidRuntime(7997): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1785)