android从xml文件中获取nodelist属性

时间:2012-04-16 03:18:06

标签: android xml-parsing android-listview

我从xml文件http://view-source:http://www.macetlagi.com/maps/st/canvaser/3/tb/tb123创建listview。我将从“segment”标签获取元素。当我运行并调试我的代码时,我收到此错误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);
  }
 }

0 个答案:

没有答案