我在以下活动中遇到了意外的令牌错误和空指针异常,我无法找出问题所在。我正在解析以下 URL,它是一个包含临时高尔夫选手详细信息的 XML 文件。当文件中的节点较少时,我让它工作,但现在我收到导致空指针的令牌错误。谁能指出哪里出错了?
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://dl.dropbox.com/u/90179278/helloworld.xml";
// XML node keys
static final String KEY_PLAYER = "player"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "playername";
static final String KEY_DOB = "dob";
static final String KEY_WEIGHT = "weight";
static final String KEY_HEIGHT = "height";
static final String KEY_HANDED = "handed";
static final String KEY_COUNTRY = "country";
static final String KEY_BIO = "bio";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_PLAYER);
// looping through all item nodes <item>
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_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_DOB, parser.getValue(e, KEY_DOB));
map.put(KEY_WEIGHT, parser.getValue(e, KEY_WEIGHT));
map.put(KEY_HEIGHT, parser.getValue(e, KEY_HEIGHT));
map.put(KEY_HANDED, parser.getValue(e, KEY_HANDED));
map.put(KEY_COUNTRY, parser.getValue(e, KEY_COUNTRY));
map.put(KEY_BIO, parser.getValue(e, KEY_BIO));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DOB, KEY_WEIGHT, KEY_HEIGHT, KEY_HANDED, KEY_COUNTRY, KEY_BIO }, new int[] {
R.id.name, R.id.dob, R.id.weight, R.id.height, R.id.handed, R.id.country, R.id.bio });
setListAdapter(adapter);
}
}
输出:
03-30 11:07:48.060: E/Error:(1742): Unexpected token (position:TEXT @1:4 in java.io.StringReader@b514c300)
03-30 11:07:48.070: D/AndroidRuntime(1742): 关闭 VM
03-30 11:07:48.070: W/dalvikvm(1742): threadid=1: 线程以未捕获的异常退出 (group=0xb4e15908)
03-30 11:07:48.090:E/AndroidRuntime(1742):致命异常:主要
03-30 11:07:48.090: E/AndroidRuntime(1742): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.AndroidXMLParsingActivity}: java.lang.NullPointerException
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.app.ActivityThread.access$600(ActivityThread.java:141)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.os.Handler.dispatchMessage(Handler.java:99)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.os.Looper.loop(Looper.java:137) 03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android. app.ActivityThread.main(ActivityThread.java:5039) 03-30 11:07:48.090: E/AndroidRuntime(1742): at java.lang.reflect.Method.invokeNative(Native Method) 03-30 11:07:48.090 : E/AndroidRuntime(1742): at java.lang.reflect.Method.invoke(Method.java:511) 03-30 11:07:48.090: E/AndroidRuntime(1742): at com.android.internal.os。 ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-30 11:07:48.090: E/AndroidRuntime(1742): at dalvik.system.NativeStart.main(Native Method)
03-30 11:07:48.090:E/AndroidRuntime(1742):由:java.lang.NullPointerException 引起
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:47)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.app.Activity.performCreate(Activity.java:5104)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-30 11:07:48.090: E/AndroidRuntime(1742): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-30 11:07:48.090: E/AndroidRuntime(1742): ... 11 更多