无法使用SAX Parser检索数据

时间:2016-04-03 19:59:32

标签: android

我正在尝试从服务器解析XML文件,我同意了这个tutorial,但我无法在Logcat中检索活动中的数据!
它在此行显示NullPointerException for(int i = 0; i < data.getTitle().size(); i++) 根据教程我不知道我是否在活动中正确地推断了变量数据。 所以这是我的 MainActivity

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    public class MainActivity extends AppCompatActivity {
TextView title[];
TextView artist[];
TextView country[];
TextView company[];
TextView price[];
TextView year[];

public static XMLGettersSetters data ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    View layout = findViewById(R.id.layout);
    for (int i = 0; i < data.getTitle().size(); i++) {

        title[i] = new TextView(this);
        title[i].setText("Title= " + data.getTitle().get(i));

        artist[i] = new TextView(this);
        artist[i].setText("Artist=" + data.getArtist().get(i));

        company[i] = new TextView(this);
        company[i].setText("Company=" + data.getCompany().get(i));

        country[i] = new TextView(this);
        country[i].setText("Country=" + data.getCountry().get(i));

        price[i] = new TextView(this);
        price[i].setText("Price=" + data.getPrice().get(i));


        year[i] = new TextView(this);
        year[i].setText("Year=" + data.getYear().get(i));


        ((ViewGroup) layout).addView(title[i]);
        ((ViewGroup) layout).addView(artist[i]);
        ((ViewGroup) layout).addView(company[i]);
        ((ViewGroup) layout).addView(country[i]);
        ((ViewGroup) layout).addView(price[i]);
        ((ViewGroup) layout).addView(year[i]);

    }



}

}

XMLHandler

    import org.xml.sax.Attributes;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.DefaultHandler;
    import java.net.URL;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    public class XMLHandler extends DefaultHandler {

String elementValue = 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;

    if (localName.equals("CATALOG")) {
        data = new XMLGettersSetters();
    } else if (localName.equals("CD")) {
        /**
         * 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 = attributes.getValue("attr");
         * data.setAttribute(attributeValue);
         *
         * */
        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://www.xmlfiles.com/examples/cd_catalog.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()));

            //  View layout = findViewById(R.id.layout);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}



/**
 * 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("title"))
        data.setTitle(elementValue);
    else if (localName.equalsIgnoreCase("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 = new String(ch, start, length);
        elementOn = false;
    }

}

}

XmlGettersSetters

    import android.util.Log;
    import java.util.ArrayList;

    public class XMLGettersSetters {
    //company
    private ArrayList<String> company = new ArrayList<String>();
    public ArrayList<String> getCompany() {
    return company;
    }
     public void setCompany(String company) {
    this.company.add(company);
    Log.i("This is the company:", company);
    }
    //title
    private ArrayList<String> title = new ArrayList<String>();
    public ArrayList<String> getTitle() {
    return title;
     }
    public void setTitle(String title) {
    this.title.add(title);
    Log.i("This is the title:", title);
    }
    //artist
    private ArrayList<String> artist = new ArrayList<String>();
    public ArrayList<String> getArtist() {
    return artist;
     }
     public void setArtist(String artist) {
    this.artist.add(artist);
    Log.i("This is the artist:", artist);
    }
    //country
     private ArrayList<String> country = new ArrayList<String>();
    public ArrayList<String> getCountry() {
    return country;
    }
    public void setCountry(String country) {
    this.country.add(country);
    Log.i("This is the country:", country);
     }
    //price
     private ArrayList<String> price = new ArrayList<String>();
    public ArrayList<String> getPrice() {
    return price;
     }
    public void setPrice(String price) {
    this.price.add(price);
    Log.i("This is the price:", price);
    }

    //year
     private ArrayList<String> year = new ArrayList<String>();
    public ArrayList<String> getYear() {
    return year;
    }
    public void setYear(String year) {
    this.year.add(year);
    Log.i("This is the Year:", year);
    }






    }

1 个答案:

答案 0 :(得分:0)

据我所知,您从未在MainActivity中初始化data字段。而且,使它静止似乎是一个坏主意。 Java静态几乎总是一个坏主意,除非你真的知道你需要它。