2 flying rabbit flying_rabbit 于 2015.07.28 14:27 提问

菜鸟android 照着视频写代码出现NullPointerException 跪求大神解决

package com.example.mp3player;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.SAXParserFactory;

import com.example.download.HttpDownloader;
import com.example.model.Mp3Info;
import com.example.xml.Mp3ListContentHandler;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Mp3ListActivity extends ListActivity {
private static final int UPDATE = 1;
private static final int ABOUT = 2;

public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add(0, UPDATE, 1, R.string.mp3_list_update);
    menu.add(0, ABOUT, 2, R.string.mp3_list_about);
    return super.onCreateOptionsMenu(menu);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mp3_list);
    StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == UPDATE) {
        String xml = downloadXML("http://192.168.1.102:8080/resource.xml");

        List<Mp3Info> mp3Infos= parse(xml);
        List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

        **_下一行为空指针错误_**
            for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
                Mp3Info mp3Info = (Mp3Info) iterator.next();
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("mp3_name", mp3Info.getMp3Name());
                map.put("mp3_size", mp3Info.getMp3Size());
                list.add(map);
            }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,R.layout.mp3info_item, new String[] { "mp3_name", "mp3_size" },new int[] { R.id.mp3_name, R.id.mp3_size });
        setListAdapter(simpleAdapter);
    }
    else if (item.getItemId() == ABOUT) {
    }
    return super.onOptionsItemSelected(item);
}
private String downloadXML(String urlStr) {
    HttpDownloader httpDownloader = new HttpDownloader();
    String result = httpDownloader.download(urlStr);
    return result;
}

private List<Mp3Info> parse(String xmlStr) {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    try {
        List<Mp3Info> infos = new ArrayList<Mp3Info>();
        XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
        Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);
        xmlReader.setContentHandler(mp3ListContentHandler);
        xmlReader.parse(new InputSource(new StringReader(xmlStr)));
        for (Iterator iterator = infos.iterator(); iterator.hasNext();) {
            Mp3Info mp3Info = (Mp3Info) iterator.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

2个回答

danielinbiti
danielinbiti   Ds   Rxr 2015.07.28 15:02
已采纳
private List<Mp3Info> parse(String xmlStr) {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    try {
        List<Mp3Info> infos = new ArrayList<Mp3Info>();
        XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
        Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);
        xmlReader.setContentHandler(mp3ListContentHandler);
        xmlReader.parse(new InputSource(new StringReader(xmlStr)));
        for (Iterator iterator = infos.iterator(); iterator.hasNext();) {
            Mp3Info mp3Info = (Mp3Info) iterator.next();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
没有返回值,改成
private List<Mp3Info> parse(String xmlStr) {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    try {
        List<Mp3Info> infos = new ArrayList<Mp3Info>();
        XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
        Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);
        xmlReader.setContentHandler(mp3ListContentHandler);
        xmlReader.parse(new InputSource(new StringReader(xmlStr)));
        for (Iterator iterator = infos.iterator(); iterator.hasNext();) {
            Mp3Info mp3Info = (Mp3Info) iterator.next();
        }
                return infos;//增加返回值
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
flying_rabbit
flying_rabbit 忘记了返回值 太感谢了!!
2 年多之前 回复
sina_2831808769
sina_2831808769   Rxr 2015.07.28 20:28

return infos;//增加返回值 这个不加代码不会出错吗,java是会的

flying_rabbit
flying_rabbit 不加返回值 的确是错的
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片