2012-03-19 9 views
1

生成します。私はこのコードを使用していますSAX解析は、私は、そのXMLを解析し、S​​Dカードに保存し、サーバーからXMLを取得するnullポインタ例外

03-19 13:53:26.943: E/AndroidRuntime(12512): FATAL EXCEPTION: main 
03-19 13:53:26.943: E/AndroidRuntime(12512): java.lang.NullPointerException. 

/** Create Object For SiteList Class */ 
SitesList sitesList = null; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    DownloadFromUrl("http://www.androidpeople.com/wp- content/uploads/2010/06/example.xml","example.xml"); 
    /** Create a new layout to display the view */ 
    LinearLayout layout = new LinearLayout(this); 
    layout.setOrientation(1); 
    /** Create a new textview array to display the results */ 
    TextView name[]; 
    TextView website[]; 
    TextView category[]; 
    try { 
     /** Handling XML */ 
     String path = Environment.getExternalStorageDirectory() +"../xmls"+"/example.xml"; 
     File file = new File(path); 
     SAXParserFactory spf = SAXParserFactory.newInstance(); 
     SAXParser sp = spf.newSAXParser(); 
     XMLReader xr = sp.getXMLReader(); 
     /** Create handler to handle XML Tags (extends DefaultHandler) */ 
     DataHandler myXMLHandler = new DataHandler(); 
     xr.setContentHandler(myXMLHandler); 
     xr.parse(new InputSource(new InputStreamReader(new FileInputStream(file))));//parse(new InputSource(sourceUrl.openStream())); 
    } catch (Exception e) { 
     System.out.println("XML Pasing Excpetion = " + e); 
    } 
    /** Get result from DataHandler SitlesList Object */ 
    sitesList = DataHandler.sitesList; 
    /** Assign textview array lenght by arraylist size */ 
    name = new TextView[sitesList.getName().size()]; 
    website = new TextView[sitesList.getWebsite().size()]; 
    category = new TextView[sitesList.getCategory().size()]; 
    /** Set the result text in textview and add it to layout */ 
    for (int i = 0; i < sitesList.getName().size(); i++) { 
     name[i] = new TextView(this); 
     name[i].setText("Name = "+sitesList.getName().get(i)); 
     website[i] = new TextView(this); 
     website[i].setText("Website = "+sitesList.getWebsite().get(i)); 
     category[i] = new TextView(this); 
     category[i].setText("Website Category = "+sitesList.getCategory().get(i)); 
     layout.addView(name[i]); 
     layout.addView(website[i]); 
     layout.addView(category[i]); 
    } 
    /** Set the layout view to display */ 
    setContentView(layout); 
} 

private void DownloadFromUrl(String DownloadUrl, String fileName) 
{ 
    URL url; 
    File root = android.os.Environment.getExternalStorageDirectory(); 
    File dir = new File (root.getAbsolutePath() + "/xmls"); 
    if(dir.exists()==false) { 
     dir.mkdirs(); 
    } 
    try { 
     url = new URL(DownloadUrl); 
     File file = new File(dir, fileName); 
     Log.d("DownloadManager", "download begining"); 
     Log.d("DownloadManager", "download url:" + url); 
     Log.d("DownloadManager", "downloaded file name:" + fileName); 
     /* Open a connection to that URL. */ 
     URLConnection ucon = url.openConnection(); 
     /* 
     * Define InputStreams to read from the URLConnection. 
     */ 
     InputStream is = ucon.getInputStream(); 
     BufferedInputStream bis = new BufferedInputStream(is); 
     /* 
     * Read bytes to the Buffer until there is nothing more to read(-1). 
     */ 
     ByteArrayBuffer baf = new ByteArrayBuffer(5000); 
     int current = 0; 
     while ((current = bis.read()) != -1) { 
      baf.append((byte) current); 
     } 
     /* Convert the Bytes read to a String. */ 
     FileOutputStream fos = new FileOutputStream(file); 
     fos.write(baf.toByteArray()); 
     fos.flush(); 
     fos.close(); 
     //Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime)/1000) + " sec"); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } //you can write here any link 
    catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

そして、私のハンドラクラスをされています。私はこの例外を取得

Boolean currentElement = false; 
Context theContext; 
String currentValue = null; 
public static SitesList sitesList = null; 

public static SitesList getSitesList() 
{ 
    return sitesList; 
} 

public static void setSitesList(SitesList sitesList) 
{ 
    DataHandler.sitesList = sitesList; 
} 

@Override 
public void startElement(String uri, String localName, String qName, 
         Attributes attributes) throws SAXException 
{ 
    super.startElement(uri, localName, qName, attributes); 
    currentElement = true; 
    if (localName.equals("maintag")) 
    { 
     /** Start */ 
     sitesList = new SitesList(); 
    } 
    else if (localName.equals("website")) 
    { 
     /** Get attribute value */ 
     String attr = attributes.getValue("category"); 
     sitesList.setCategory(attr); 
    } 
} 

@Override 
public void endElement(String uri, String localName, String qName) 
    throws SAXException { 
    super.endElement(uri, localName, qName); 
    currentElement = false; 
    /** set value */ 
    if (localName.equalsIgnoreCase("name")) 
     sitesList.setName(currentValue); 
    else if (localName.equalsIgnoreCase("website")) 
     sitesList.setWebsite(currentValue); 
} 

@Override 
public void characters(char[] ch, int start, int length) 
    throws SAXException { 
// TODO Auto-generated method stub 
    super.characters(ch, start, length); 
    if (currentElement) 
    { 
     currentValue = new String(ch, start, length); 
     currentElement = false; 
    } 
} 

そして、私のリストクラスは次のとおりです。

/** Variables */ 
private ArrayList name = new ArrayList(); 
private ArrayList website = new ArrayList(); 
private ArrayList category = new ArrayList(); 

/** In Setter method default it will return arraylist 
* change that to add */ 
public ArrayList getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name.add(name); 
} 

public ArrayList getWebsite() { 
    return website; 
} 
public void setWebsite(String website) { 
    this.website.add(website); 
} 

public ArrayList getCategory() { 
    return category; 
} 
public void setCategory(String category) { 
    this.category.add(category); 
} 

XMLファイルがダウンロードされ、SDカードに保存されます。動作しますが、解析されません。

+2

ここで例外が発生し、常に置くフルlogcat ...... –

+0

は... occure problem.Iは「../xmls」は削除し、それを固定することができます「..」それは働いてます。.. – learner

+2

[はあなたのポストでの説明が含まれ、単にリンクと答えていません] –

答えて

0

SAX(XMLのためのシンプルなAPI)は、XML文書のためのXML-DEVメーリングリストによって開発されたイベントベースのシーケンシャルアクセスパーサーAPIです。 SAXは、Document Object Model(DOM)によって提供されるデータの代わりにXMLドキュメントからデータを読み込むためのメカニズムを提供します。 DOMがドキュメント全体で動作する場合、SAXパーサーはXMLドキュメントの各部分に対して順次動作します。

解決方法はlinkを参照してください。

お手伝いします。私はそれを先生を修正することができます

+2

>してください、私は誰もが解決のためにここにある知っているが、私は、その後どのように誰もがそれを解決できるかprblm知らない(http://stackoverflow.com/questions/ハウツー・トゥ・アンサー)。 – Gilles

+2

@Prem - 答えが累積しているので、できるだけ早く回答を拡大することを検討してください。 – slugster

+0

@Prem:「私は...心の中でそれを維持するだろう」 - >編集このポストをし、それについて何かをします。 – Matt