我已经按照教程向您展示了如何连接到RSS源,并将数据下拉到listView中。问题是,我希望能够循环遍历多个源,将它们添加到同一个listView,但是当我尝试这样做时......它会带来以下logCat错误。
如果有人可以帮助我,那将非常感谢!
代码:
package com.androidhive.xmlparsing;
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.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
//static final String URL = "http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=uk";
String[] URL = new String[2];
int count = 0;
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rssRun();
}
public void rssRun()
{
URL[0] = "http://www.skysports.com/rss/0,20514,11661,00.xml";
URL[1] = "http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=uk";
for (int f= 0;f < 2;f++)
{
count+=1;
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL[f]); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
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_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.name, R.id.desciption, R.id.cost });
if (count==2)
{
setListAdapter(adapter);
}
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
}
}
logcat的:
08-27 09:34:39.786: E/Error:(30651): Expected a quoted string (position:DOCDECL @1:50 in java.io.StringReader@42be4218)
08-27 09:34:39.786: D/AndroidRuntime(30651): Shutting down VM
08-27 09:34:39.786: W/dalvikvm(30651): threadid=1: thread exiting with uncaught exception (group=0x41966da0)
08-27 09:34:39.786: E/AndroidRuntime(30651): FATAL EXCEPTION: main
08-27 09:34:39.786: E/AndroidRuntime(30651): Process: com.androidhive.xmlparsing, PID: 30651
08-27 09:34:39.786: E/AndroidRuntime(30651): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.xmlparsing/com.androidhive.xmlparsing.AndroidXMLParsingActivity}: java.lang.NullPointerException
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.app.ActivityThread.access$900(ActivityThread.java:161)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.os.Handler.dispatchMessage(Handler.java:102)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.os.Looper.loop(Looper.java:157)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.app.ActivityThread.main(ActivityThread.java:5356)
08-27 09:34:39.786: E/AndroidRuntime(30651): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 09:34:39.786: E/AndroidRuntime(30651): at java.lang.reflect.Method.invoke(Method.java:515)
08-27 09:34:39.786: E/AndroidRuntime(30651): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-27 09:34:39.786: E/AndroidRuntime(30651): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-27 09:34:39.786: E/AndroidRuntime(30651): at dalvik.system.NativeStart.main(Native Method)
08-27 09:34:39.786: E/AndroidRuntime(30651): Caused by: java.lang.NullPointerException
08-27 09:34:39.786: E/AndroidRuntime(30651): at com.androidhive.xmlparsing.AndroidXMLParsingActivity.rssRun(AndroidXMLParsingActivity.java:57)
08-27 09:34:39.786: E/AndroidRuntime(30651): at com.androidhive.xmlparsing.AndroidXMLParsingActivity.onCreate(AndroidXMLParsingActivity.java:38)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.app.Activity.performCreate(Activity.java:5426)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
08-27 09:34:39.786: E/AndroidRuntime(30651): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)