public class ResultsTitre extends ActionBarActivity {
org.w3c.dom.Document doc = null;
EditText resultActors;
EditText resultAnnee;
EditText resultGenre;
EditText resultPlot;
EditText resultRating;
EditText resultReleased;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results_titre);
resultActors=(EditText)findViewById(R.id.resultActors);
resultActors.setEnabled(false);
resultAnnee=(EditText)findViewById(R.id.resultAnnee);
resultAnnee.setEnabled(false);
resultGenre=(EditText)findViewById(R.id.resultGenre);
resultGenre.setEnabled(false);
resultPlot=(EditText)findViewById(R.id.resultPlot);
resultPlot.setEnabled(false);
resultRating=(EditText)findViewById(R.id.resultRating);
resultRating.setEnabled(false);
resultReleased=(EditText)findViewById(R.id.resultReleased);
resultReleased.setEnabled(false);
new xmlFromHttpAsyncTask().execute("http://www.omdbapi.com/?t="+ titleTyped +"&r=xml");
}
private class xmlFromHttpAsyncTask extends AsyncTask<String, Void, Void> {
protected Void doInBackground(String... query) {
URL url;
try {
// La on crÈe la requete HTTP
url = new URL(query[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Et on rÈcupËre la rÈponse de OMDBAPI
doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
//Traitement des exceptions au cas o˘ ca plante, on s'en fout
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void n) {
createDisplay();
}
}
private void createDisplay(){
try {
NodeList nodeList = doc.getElementsByTagName("root");
Element rootElement = (Element) nodeList.item(0);
NodeList movieList = rootElement.getElementsByTagName("Movie");
Element e = (Element)movieList.item(0);
resultActors.setText(e.getAttribute("actors"));
resultAnnee.setText(e.getAttribute("year"));
resultGenre.setText(e.getAttribute("genre"));
resultPlot.setText(e.getAttribute("plot"));
resultRating.setText(e.getAttribute("imdbRating"));
resultReleased.setText(e.getAttribute("released"));
e.getAttribute("imdbID");
}
catch (Exception ex){
resultReleased.setText("ERROR : " + ex);
}
}
}
当我编译这段代码时,我得到了&#34; java.lang.nullpointerexception&#34;在我的最后一次抓!我不明白为什么!有人可以帮忙吗?
我的XML文件看起来像这样
<root response="True">
<movie title="Icarus" year="2010" rated="TV-14" released="10 Dec 2010" runtime="42 min"
genre="Adventure, Drama, Romance" director="Mairzee Almas"
writer="Jerry Siegel (character created by: Superman), Joe Shuster (character created by: Superman), Alfred Gough (developed for television by), Miles Millar (developed for television by), Genevieve Sparling"
actors="Tom Welling, Erica Durance, Cassidy Freeman, Justin Hartley"
plot="As the VRA threat intensifies, Clark takes initiative by closing down watchtower and declaring the League go officially underground, but will this be enough to stop trotter and Slade Wilson..."
language="English" country="USA" awards="N/A"
poster="http://ia.media-imdb.com/images/M/MV5BMTQ0MTg0MTc0Ml5BMl5BanBnXkFtZTgwNjk5ODEyMjE@._V1_SX300.jpg"
metascore="N/A" imdbRating="8.6" imdbVotes="393" imdbID="tt1628582" type="episode"/>
</root>
答案 0 :(得分:0)
DOM区分大小写,因此对rootElement.getElementsByTagName("Movie")
的调用将始终返回null,因为xml包含带有小写的“movie”。