【问题标题】:simpleAdapter ArrayAdapter null pointer exceptionsimpleAdapter ArrayAdapter 空指针异常
【发布时间】:2013-11-10 20:36:50
【问题描述】:

由于某种原因,此代码会抛出 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

               }
            }

.......

【问题讨论】:

  • 学习使用调试器和/或日志来查明此类错误。

标签: java android nullpointerexception android-arrayadapter simpleadapter


【解决方案1】:

问题解决了!!!

myList.setAdapter(adapter);

必须成为

setListAdapter(adapter);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    相关资源
    最近更新 更多