我正在使用自定义列表视图运行rss feed解析程序。
现在我的这个程序在Windows 7上与android sdk eclipse Indigo一起工作正常, 但是同样的代码在Ubuntu系统(11.04)上没有使用我的android sdk-Eclipse(Indigo)。
所有其他程序都运行得很好。
这是我在logcat中遇到的错误。 (由 java.lang.NullPointerException 引起的 java.lang.RuntimeException )
05-16 14:20:24.965: E/AndroidRuntime(445): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.CustomizedListView}: java.lang.NullPointerException
05-16 14:20:24.965: E/AndroidRuntime(445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-16 14:20:24.965: E/AndroidRuntime(445): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-16 14:20:24.965: E/AndroidRuntime(445): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-16 14:20:24.965: E/AndroidRuntime(445): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-16 14:20:24.965: E/AndroidRuntime(445): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 14:20:24.965: E/AndroidRuntime(445): at android.os.Looper.loop(Looper.java:123)
05-16 14:20:24.965: E/AndroidRuntime(445): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-16 14:20:24.965: E/AndroidRuntime(445): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 14:20:24.965: E/AndroidRuntime(445): at java.lang.reflect.Method.invoke(Method.java:521)
05-16 14:20:24.965: E/AndroidRuntime(445): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-16 14:20:24.965: E/AndroidRuntime(445): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-16 14:20:24.965: E/AndroidRuntime(445): at dalvik.system.NativeStart.main(Native Method)
05-16 14:20:24.965: E/AndroidRuntime(445): Caused by: java.lang.NullPointerException
05-16 14:20:24.965: E/AndroidRuntime(445): at com.example.androidhive.CustomizedListView.onCreate(CustomizedListView.java:44)
05-16 14:20:24.965: E/AndroidRuntime(445): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-16 14:20:24.965: E/AndroidRuntime(445): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-16 14:20:24.965: E/AndroidRuntime(445): ... 11 more
那么这个问题的解决方案是什么,任何想法?
感谢收听。
这是我的代码。
cusomizedlistview.java
static final String URL = "http://api.androidhive.info/music/music.xml";
//static final String URL = "https://news.google.com/news/feeds?ned=in&topic=n&output=rss";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> songsList = 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
NodeList nl = doc.getElementsByTagName(KEY_SONG);
// 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_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
}
Xmlparser.java
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;
}