0

NullPointerException这段代码出于某种原因抛出了一个。我不知道为什么。有人可以帮忙吗?

static final String KEY_NEWS = "news"; // parent node

static final String KEY_ID = "id";

public static final String KEY_TITLOS = "titlos";
public static final String KEY_DATE = "date";
public static final String KEY_FOTO = "foto";
public static final String KEY_MESSAGE = "message";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rss_item_list);
try {               

        if (android.os.Environment
                  .getExternalStorageState()
                  .equals(android.os.Environment.MEDIA_MOUNTED))
            {

            if(!XmlDir.exists())
                XmlDir.mkdirs();

            if(isOnline()){


            File file = new File(XmlDir,"news.xml");
            XMLParser.DownloadFile(data, file);

代码继续这样

    InputStream is = new FileInputStream(file.getPath());
    DocumentBuilderFactory dbf =            DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(new InputSource(is));
    doc.getDocumentElement().normalize();

    XMLParser parser = new XMLParser();

NodeList nl = doc.getElementsByTagName(KEY_NEWS);
    // looping through all hotels nodes <hotel>
    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_TITLOS, parser.getValue(e, KEY_TITLOS));
        map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
        map.put(KEY_FOTO, parser.getValue(e, KEY_FOTO));
        map.put(KEY_MESSAGE, parser.getValue(e, KEY_MESSAGE));


        // adding HashList to ArrayList
        HotelList.add(map);
    }

我认为错误出在以下代码中:

ListView myList=(ListView)findViewById(R.id.list);

…………

    ListAdapter adapter = new SimpleAdapter(MyNews.this,
    HotelList, R.layout.rss_item_list_row,
    new String[] { KEY_FOTO, KEY_TITLOS, KEY_DATE, KEY_MESSAGE },
    new int[] { R.id.page_url, R.id.title, R.id.pub_date, R.id.message });


    myList.setAdapter(adapter);

 // Click event for single list row

               }
            }

…………

4

1 回答 1

0

Problem solved!!!

myList.setAdapter(adapter);

must become

setListAdapter(adapter);
于 2013-11-21T19:49:19.100 回答