Pregunta

This code throws a NullPointerException for some reason. I cannot find out why. Can anybody help?

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);

the code continues like this

    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);
    }

I think that the mistake is somewhere in the following code:

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

               }
            }

.......

¿Fue útil?

Solución

Problem solved!!!

myList.setAdapter(adapter);

must become

setListAdapter(adapter);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top