0

XPath を使用して ListView に XML コンテンツを表示する単純な Android アプリを作成しています。私のコードは下にあります。null例外をキャッチしています。助けてください:( Googleとさまざまなチュートリアルを試しましたが、どれも私の問題を解決していないようです:(

public class MainActivity extends ListActivity {

ArrayList<String> mPeople = new ArrayList<String>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    try {
        parseData();
    } catch(Exception ex) {
        Toast.makeText(this, "Exception: " + ex.getMessage(), Toast.LENGTH_LONG).show();
        System.out.println(ex.getMessage() +"==============");

    }

    // pass adapter w/ data queried through XPath to ListView
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mPeople);
    setListAdapter(adapter);
}

private void parseData() throws Exception {    
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();

Document doc = db.parse(new URL("http://www.w3schools.com/xpath/books.xml").openStream());
// query XPath instance, this is the parser
XPath xpath = XPathFactory.newInstance().newXPath();
// specify the xpath expression
// String expression = " /bookstore/book/title ";

XPathExpression e = xpath.compile("/bookstore/book/title");

// list of nodes queried
NodeList nodes = (NodeList)e.evaluate(doc, XPathConstants.NODESET);

Toast.makeText(this, "count: " + String.valueOf(nodes.getLength()),Toast.LENGTH_SHORT).show();
// if node found
if(nodes != null && nodes.getLength() > 0) {
  mPeople.clear();
  int len = nodes.getLength();
  for(int i = 0; i < len; ++i) {
      // query value
      Node node = nodes.item(i);
      mPeople.add(node.getTextContent());
  }
}
}
}

私が得る出力は、Toast を含む Exception: null です。openStream()indb.parse()が戻ってくると思いますnull。誰かが解決策を見つけたら、私に知らせてください。ありがとう。

編集:これがLogcatです

06-01 10:25:42.586: I/System.out(1401): null==============
06-01 10:25:45.721: I/Choreographer(1401): Skipped 210 frames!  The application may be doing too much work on its main thread.
06-01 10:25:47.226: I/Choreographer(1401): Skipped 1768 frames!  The application may be doing too much work on its main thread.
06-01 10:25:47.467: D/gralloc_goldfish(1401): Emulator without GPU emulation detected.
4

1 に答える 1