2016-04-03 54 views
0

我試圖解析來自服務器的XML文件,我與此tutorial同仁,但我無法在Logcat中的活動中檢索數據!
它顯示了在這一行
for(int i = 0; i < data.getTitle().size(); i++) 根據該教程,我不知道如果我intialised變量數據正確的活動一個NullPointerException。 所以這是我MainActivity無法使用SAX解析器檢索數據

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); 
    } 






    } 

回答

0

據我所知,您從未初始化MainActivity中的data字段。另外,使它靜態似乎是一個壞主意。除非你真的知道你需要Java靜態,否則Java靜態幾乎總是一個壞主意。