1

我正在运行一个带有自定义列表视图的 rss 提要解析程序。

现在我的这个程序可以在 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

那么这种问题的解决方案是什么,有什么想法吗?

谢谢收听。

这是我的代码。

自定义列表视图.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;
}
4

0 回答 0