我正在编写一个程序,其中我使用XMLParser来解析DOM元素中的数据,但每当我运行我的程序时:不幸的是,应用程序已停止
Logcat说:
03-18 10:41:11.266: I/System.out(843): Getting DOM Element(s):- null
03-18 10:41:11.266: D/AndroidRuntime(843): Shutting down VM
03-18 10:41:11.266: W/dalvikvm(843): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
03-18 10:41:11.287: E/AndroidRuntime(843): FATAL EXCEPTION: main
03-18 10:41:11.287: E/AndroidRuntime(843): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.CustomizedListViewPackage}: java.lang.NullPointerException
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.os.Looper.loop(Looper.java:137)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.main(ActivityThread.java:5039)
03-18 10:41:11.287: E/AndroidRuntime(843): at java.lang.reflect.Method.invokeNative(Native Method)
03-18 10:41:11.287: E/AndroidRuntime(843): at java.lang.reflect.Method.invoke(Method.java:511)
03-18 10:41:11.287: E/AndroidRuntime(843): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-18 10:41:11.287: E/AndroidRuntime(843): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-18 10:41:11.287: E/AndroidRuntime(843): at dalvik.system.NativeStart.main(Native Method)
03-18 10:41:11.287: E/AndroidRuntime(843): Caused by: java.lang.NullPointerException
03-18 10:41:11.287: E/AndroidRuntime(843): at com.example.androidhive.CustomizedListViewPackage.onCreate(CustomizedListViewPackage.java:52)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.Activity.performCreate(Activity.java:5104)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-18 10:41:11.287: E/AndroidRuntime(843): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-18 10:41:11.287: E/AndroidRuntime(843): ... 11 more
CustomizedListViewPackage.java:
import java.util.ArrayList;
import java.util.HashMap;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
public class CustomizedListViewPackage extends Activity {
// All static variables
static final String URL = "http://erachnida.net/android/packages.xml";
// XML node keys
static final String KEY_SONGS = "songs"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_OFFER = "offer";
static final String KEY_DETAIL = "detail";
static final String KEY_CHECKS = "checks";
static final String KEY_DRINK = "drink";
static final String KEY_SERVICE = "service";
static final String KEY_FOOD = "food";
static final String KEY_INTERNET = "internet";
static final String KEY_RATES = "rates";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
ListView lists;
LazyAdapterPackage adapters;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_package);
final ArrayList<HashMap<String, String>> songsLists = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
System.out.println("Getting DOM Element(s):- " + doc); // getting null here
// Getting null pointer in below line(line number - 52)
NodeList nl = doc.getElementsByTagName(KEY_SONGS);
// looping through all song nodes <song>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_OFFER, parser.getValue(e, KEY_OFFER));
map.put(KEY_DETAIL, parser.getValue(e, KEY_DETAIL));
map.put(KEY_CHECKS, parser.getValue(e, KEY_CHECKS));
map.put(KEY_DRINK, parser.getValue(e, KEY_DRINK));
map.put(KEY_SERVICE, parser.getValue(e, KEY_SERVICE));
map.put(KEY_FOOD, parser.getValue(e, KEY_FOOD));
map.put(KEY_INTERNET, parser.getValue(e, KEY_INTERNET));
map.put(KEY_RATES, parser.getValue(e, KEY_RATES));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsLists.add(map);
}
lists=(ListView)findViewById(R.id.list);
// Getting adapter by passing xml data ArrayList
adapters=new LazyAdapterPackage(this, songsLists);
lists.setAdapter(adapters);
// Click event for single list row
lists.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
HashMap<String, String> map = songsLists.get(position);
Intent in = new Intent(CustomizedListViewPackage.this,com.example.androidhive.SingleViewPackage.class);
in.putExtra(KEY_TITLE, map.get(KEY_TITLE));
in.putExtra(KEY_OFFER, map.get(KEY_OFFER));
in.putExtra(KEY_DETAIL, map.get(KEY_DETAIL));
in.putExtra(KEY_CHECKS, map.get(KEY_CHECKS));
in.putExtra(KEY_DRINK, map.get(KEY_DRINK));
in.putExtra(KEY_SERVICE, map.get(KEY_SERVICE));
in.putExtra(KEY_FOOD, map.get(KEY_FOOD));
in.putExtra(KEY_INTERNET, map.get(KEY_INTERNET));
in.putExtra(KEY_RATES, map.get(KEY_RATES));
in.putExtra(KEY_DURATION, map.get(KEY_DURATION));
in.putExtra(KEY_THUMB_URL, map.get(KEY_THUMB_URL));
startActivity(in);
}
});
}
}
见以下代码XMLParser.java:
public class XMLParser {
// constructor
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
* @param XML string
* */
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/** Getting node value
* @param elem element
*/
public final String getElementValue( Node elem ) {
Node child;
if( elem != null){
if (elem.hasChildNodes()){
for( child = elem.getFirstChild(); child != null; child = child.getNextSibling() ){
if( child.getNodeType() == Node.TEXT_NODE ){
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
* @param Element node
* @param key string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
在几个ListView类中使用单个XMLParser类,就像这个应用程序中的CustomizedListView.java和CustomizedListViewPackage.java
试试这个
String xml = parser.getXmlFromUrl(URL);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(xml);
Element root = dom.getDocumentElement();
日志:
获取DOM元素:- null
表示没有从服务器获取数据。将getXmlFromUrl
方法更改为使用获取InputStream
,然后从中读取数据为String:
public String getXmlFromUrl(String url) {
String xml = null;
InputStream content = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
content = response.getEntity().getContent();
} catch (Exception e) {
e.printStackTrace();
}
// return XML
return inputStreamToString(content).toString();
}
private StringBuilder inputStreamToString(InputStream is) {
String line = "";
StringBuilder xml = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
xml.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xml;
}