1

嗨,我有以下代码,它总是导致空指针异常:

protected void onPostExecute(List<String> result) {     
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getApplicationContext(),R.id.spinner1);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Iterator<String> it = result.iterator();

    while(it.hasNext()){
        String v = (String) it.next();
        adapter.add(v);
    }

    Spinner spin = (Spinner) findViewById(R.id.spinner1);
    spin.setAdapter(adapter); //this line
}

如果我注释该this line行,则不会引发异常,并且我已经检查过并且 spin 不为 null,适配器也不为空。

为什么我仍然收到以下错误?

12-13 05:08:55.446: E/AndroidRuntime(1349): android.content.res.Resources$NotFoundException: Resource ID #0x7f070002 type #0x12 is not valid
12-13 05:08:55.446: E/AndroidRuntime(1349): at android.content.res.Resources.loadXmlResourceParser(Resources.java:2110)
4

1 回答 1

2

您需要将布局资源传递给 ArrayAdapter 构造函数,例如:

new ArrayAdapter<CharSequence>(getApplicationContext(), 
        android.R.layout.simple_spinner_item);
于 2012-12-12T21:22:06.520 回答