flying_rabbit 2015-07-28 06:27 采纳率: 100%
浏览 1521
已采纳

菜鸟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 2015-07-28 07: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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求一个图片中的成交量选股公式
  • ¥30 关于#python#的问题:我想要的是这79个大特征对于房屋售价的最大的影响前十名(相关搜索:随机森林)
  • ¥15 使用matlab计算自定义特殊函数的二重积分,改变积分顺序所得的结果不同的问题?
  • ¥15 mysql做碎片化处理老是报错怎么办
  • ¥15 如何正确在vs2010中初始化map对象
  • ¥30 mmdet3d模型部署问题
  • ¥15 comsol仿真反射率、吸收率时峰值位置和深度不对!
  • ¥30 Visual Studio找不到sdk,如何解决?
  • ¥15 labview与机器之间网口通信发送指令
  • ¥15 迪杰斯特拉算法,考虑时延,带宽