我有这段代码,它必须获取一个列表视图并用数组适配器填充它:
public class news extends ListActivity {
public String[] titoli = new String[99];
public String[] descrizioni = new String[99];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new connection().execute();
System.out.println("prova1");
}
public class connection extends AsyncTask<Void, Void, Void> {
public Void doInBackground(Void... params) {
System.out.println("prova2");
// All static variables
final String URL = "http://www.messedaglia.it/index.php/archivio-news?format=feed&type=rss";
// XML node keys
final String ITEM = "item"; // parent node
final String TITLE = "title";
final String DESC = "description";
Element e = null;
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(TITLE, parser.getValue(e, TITLE));
map.put(DESC, parser.getValue(e, DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
System.out.println("prova3");
for (int c = 0; c < nl.getLength(); c++) {
e = (Element) nl.item(c);
titoli[c] = parser.getValue(e, TITLE); // name child value
descrizioni[c] = parser.getValue(e, DESC);
}
return null;
}
public void onPostExecute() {
System.out.println("prova4");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(news.this,
android.R.layout.simple_list_item_1, titoli);
System.out.println("prova5");
ListView listView = (ListView) news.this
.findViewById(android.R.id.list);
listView.setAdapter(adapter);
}
}
}
日志在此处显示空指针异常(我的代码的第 33 行):
listView.setAdapter(adapter);
日志:
02-07 13:38:36.020: E/AndroidRuntime(13803): FATAL EXCEPTION: main
02-07 13:38:36.020: E/AndroidRuntime(13803): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.messedagliavr.messeapp/com.messedagliavr.messeapp.news}: java.lang.NullPointerException
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.app.ActivityThread.access$600(ActivityThread.java:140)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.os.Looper.loop(Looper.java:137)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.app.ActivityThread.main(ActivityThread.java:4898)
02-07 13:38:36.020: E/AndroidRuntime(13803): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 13:38:36.020: E/AndroidRuntime(13803): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 13:38:36.020: E/AndroidRuntime(13803): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-07 13:38:36.020: E/AndroidRuntime(13803): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-07 13:38:36.020: E/AndroidRuntime(13803): at dalvik.system.NativeStart.main(Native Method)
02-07 13:38:36.020: E/AndroidRuntime(13803): Caused by: java.lang.NullPointerException
02-07 13:38:36.020: E/AndroidRuntime(13803): at com.messedagliavr.messeapp.news.onCreate(news.java:33)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.app.Activity.performCreate(Activity.java:5206)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
02-07 13:38:36.020: E/AndroidRuntime(13803): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
02-07 13:38:36.020: E/AndroidRuntime(13803): ... 11 more
大多数人说要添加 setContentView()。我删除了它,因为添加它我得到这个错误:
错误:java.lang.RuntimeException:您的内容必须有一个 id 属性为“android.R.id.list”的 ListView