我收到一个 XML 文档作为变量xml
,在调试器中我可以确认 XML 已被抓取并data
正确保存。现在它永远不会在我的列表中显示带有错误的崩溃。
我的代码:
if (xml != null) {
{
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_CONTACTTITLE, getValue(e, KEY_CONTACTTITLE));
map.put(KEY_NAME, getValue(e, KEY_NAME));
map.put(KEY_MAINPHONE, getValue(e, KEY_MAINPHONE));
map.put(KEY_CELLPHONE, getValue(e, KEY_CELLPHONE));
map.put(KEY_FAX, getValue(e, KEY_FAX));
map.put(KEY_EMAIL, getValue(e, KEY_EMAIL));
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(context, menuItems,
R.layout.activity_move_contacts, new String[] {
KEY_CONTACTTITLE, KEY_NAME, KEY_MAINPHONE, KEY_CELLPHONE, KEY_FAX, KEY_EMAIL },
new int[] { R.id.ContactTitle, R.id.Name,
R.id.MainPhone, R.id.CellPhone, R.id.Fax, R.id.Email });
setListAdapter(adapter);
}
}
LogCat 错误:
02-22 21:31:59.466: E/AndroidRuntime(4482): FATAL EXCEPTION: main
02-22 21:31:59.466: E/AndroidRuntime(4482): java.lang.NullPointerException
02-22 21:31:59.466: E/AndroidRuntime(4482): at android.widget.SimpleAdapter.<init>(SimpleAdapter.java:85)
02-22 21:31:59.466: E/AndroidRuntime(4482): at com.testingapp.MoveContactsActivity$contentCall.onPostExecute(MoveContactsActivity.java:120)
02-22 21:31:59.466: E/AndroidRuntime(4482): at com.testingapp.MoveContactsActivity$contentCall.onPostExecute(MoveContactsActivity.java:1)
02-22 21:31:59.466: E/AndroidRuntime(4482): at android.os.AsyncTask.finish(AsyncTask.java:631)
02-22 21:31:59.466: E/AndroidRuntime(4482): at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-22 21:31:59.466: E/AndroidRuntime(4482): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
02-22 21:31:59.466: E/AndroidRuntime(4482): at android.os.Handler.dispatchMessage(Handler.java:99)
02-22 21:31:59.466: E/AndroidRuntime(4482): at android.os.Looper.loop(Looper.java:137)
02-22 21:31:59.466: E/AndroidRuntime(4482): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-22 21:31:59.466: E/AndroidRuntime(4482): at java.lang.reflect.Method.invokeNative(Native Method)
02-22 21:31:59.466: E/AndroidRuntime(4482): at java.lang.reflect.Method.invoke(Method.java:511)
02-22 21:31:59.466: E/AndroidRuntime(4482): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-22 21:31:59.466: E/AndroidRuntime(4482): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-22 21:31:59.466: E/AndroidRuntime(4482): at dalvik.system.NativeStart.main(Native Method)
我个人没有过多地使用适配器,并且相信我可能会搞砸一些事情。