我从 xml 文件http://view-source:http://www.macetlagi.com/maps/st/canvaser/3/tb/tb123制作列表视图。我将从“段”标签中获取元素。当我运行时调试我的代码,我收到此错误java.lang.NullPointerException
。如果我进行愚蠢的编码,请更正我的代码。这是我在 android 中的 java 代码:
public class ListSegment extends ListActivity {
String URL_XML = "http://www.macetlagi.com/maps/st/canvaser/3/tb/tb123";
static final String KEY_SEGMENT = "segment";
static final String KEY_SEGMENT_ID = "segment_id";
static final String KEY_MAIN_SEGMENT = "main_segment_name";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listsegment_main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL_XML);
Document doc = parser.getDomElement(xml);
NodeList nL = doc.getElementsByTagName(KEY_SEGMENT);
for (int i = 0; i < nL.getLength(); i++) {
Node node = nL.item(i);
if(node.hasAttributes()) {
NamedNodeMap attr_id = node.getAttributes();
attr_id.getNamedItem(KEY_SEGMENT);
}
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nL.item(i);
map.put(KEY_SEGMENT_ID, parser.getValue(e, KEY_SEGMENT_ID));
map.put(KEY_MAIN_SEGMENT, parser.getValue(e, KEY_MAIN_SEGMENT));
menuItems.add(map);
}
ListAdapter adapter = new SimpleAdapter(this, menuItems, R.layout.list_item,
new String[] {KEY_SEGMENT_ID, KEY_MAIN_SEGMENT}, new int[] {
R.id.segmentid, R.id.segmentname
});
setListAdapter(adapter);
}
}