Android SAX Parser Error

bilimovva

New Member
I'm trying to make an SAX Parser for Android and started off by reading only one tag from the external XML file. I get an 'Unfortunately, -- has stopped' error. I looked at the Log file and it gives my a null reference error. My guess is the XMLAdapter class isn't working fine and I haven't been able to figure the problem out.Here's my Main activity:\[code\]package com.vint.michiganbus;import java.net.URL;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.InputSource;import org.xml.sax.XMLReader;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.TextView;import android.view.Window;import android.util.Log;public class listView extends Activity { XMLGettersSetters data; private static final String TAG = "listView"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); View layout = findViewById(R.id.layout); TextView title[]; Log.i(TAG, "data is hello"); try { /** * Create a new instance of the SAX parser **/ SAXParserFactory saxPF = SAXParserFactory.newInstance(); SAXParser saxP = saxPF.newSAXParser(); XMLReader xmlR = saxP.getXMLReader(); URL url = new URL("http://mbus.pts.umich.edu/shared/public_feed.xml"); // URL of the XML /** * Create the Handler to handle each of the XML tags. **/ XMLHandler myXMLHandler = new XMLHandler(); xmlR.setContentHandler(myXMLHandler); xmlR.parse(new InputSource(url.openStream())); } catch (Exception e) { System.out.println(e); } data = http://stackoverflow.com/questions/10685427/XMLHandler.data; /** * Makes the TextView length the size of the TextView arrays by getting the size of the **/ title = new TextView[data.get().size()]; /** * Run a for loop to set All the TextViews with text until * the size of the array is reached. * **/ for (int i = 0; i < data.get().size(); i++) { title = new TextView(this); title.setText("Title = "+data.get().get(i)); ((ViewGroup) layout).addView(title); } setContentView(layout); //setContentView(R.layout.listview); }}\[/code\]Here's my XML Handler:\[code\]package com.vint.michiganbus;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;import android.util.Log;public class XMLHandler extends DefaultHandler { private static final String TAG = "In Handler"; String elementValue = http://stackoverflow.com/questions/10685427/null; Boolean elementOn = false; public static XMLGettersSetters data = null; public static XMLGettersSetters getXMLData() { return data; } public static void setXMLData(XMLGettersSetters data) { XMLHandler.data = data; } /** * This will be called when the tags of the XML starts. **/ @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { elementOn = true; Log.i(TAG,"starting Element "+localName); if (localName.equals("livefeed")) { data = http://stackoverflow.com/questions/10685427/new XMLGettersSetters(); } else if (localName.equals("routecount")) { /** * We can get the values of attributes for eg. if the CD tag had an attribute( <CD attr= "band">Akon</CD> ) * we can get the value "band". Below is an example of how to achieve this. * * String attributeValue = http://stackoverflow.com/questions/10685427/attributes.getValue("attr"); * data.setAttribute(attributeValue); * * */ } } /** * This will be called when the tags of the XML end. **/ @Override public void endElement(String uri, String localName, String qName) throws SAXException { elementOn = false; /** * Sets the values after retrieving the values from the XML tags * */ if (localName.equalsIgnoreCase("routecount")) data.set(elementValue); /*else if (localName.equalsIgnoreCae("artist")) data.setArtist(elementValue); else if (localName.equalsIgnoreCase("country")) data.setCountry(elementValue); else if (localName.equalsIgnoreCase("company")) data.setCompany(elementValue); else if (localName.equalsIgnoreCase("price")) data.setPrice(elementValue); else if (localName.equalsIgnoreCase("year")) data.setYear(elementValue);*/ } /** * This is called to get the tags value **/ @Override public void characters(char[] ch, int start, int length) throws SAXException { if (elementOn) { elementValue = http://stackoverflow.com/questions/10685427/new String(ch, start, length); elementOn = false; } }}\[/code\]And this is my GetterSetter class\[code\]package com.vint.michiganbus;import java.util.ArrayList;public class XMLGettersSetters { private ArrayList<String> routecount = new ArrayList<String>(); public ArrayList<String> get() { return routecount; } public void set(String company) { this.routecount.add(company); }}\[/code\]Any help will be greatly appreciated!ps: If anyone has suggestions how to handle the XML in the URL efficiently I'd love that!
 
Back
Top