gpt4 book ai didi

android - XML 解析给出 NullPointerException Error with Asynctask

转载 作者:行者123 更新时间:2023-11-30 03:47:14 26 4
gpt4 key购买 nike

我正在使用 XML 解析器从网站中提取一些内容,并且我将代码放在 Asynctask 中。出于某种原因,我不断收到 NullPointerException,为什么?

是什么导致了错误,您如何解决它?非常感谢您抽出宝贵的时间。

这是 LogCat

02-08 23:34:08.835: E/AndroidRuntime(28103): FATAL EXCEPTION: main
02-08 23:34:08.835: E/AndroidRuntime(28103): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.thenewlistivew/com.thenewlistivew.MainActivity}: java.lang.NullPointerException
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.app.ActivityThread.access$600(ActivityThread.java:127)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.os.Looper.loop(Looper.java:137)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.app.ActivityThread.main(ActivityThread.java:4511)
02-08 23:34:08.835: E/AndroidRuntime(28103): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 23:34:08.835: E/AndroidRuntime(28103): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 23:34:08.835: E/AndroidRuntime(28103): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:976)
02-08 23:34:08.835: E/AndroidRuntime(28103): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:743)
02-08 23:34:08.835: E/AndroidRuntime(28103): at dalvik.system.NativeStart.main(Native Method)
02-08 23:34:08.835: E/AndroidRuntime(28103): Caused by: java.lang.NullPointerException
02-08 23:34:08.835: E/AndroidRuntime(28103): at com.thenewlistivew.MainActivity.onCreate(MainActivity.java:40)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.app.Activity.performCreate(Activity.java:4486)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
02-08 23:34:08.835: E/AndroidRuntime(28103): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
02-08 23:34:08.835: E/AndroidRuntime(28103): ... 11 more

我的 AsyncTask 代码:

private class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void> {
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

@Override
protected void onPreExecute() {
// update the UI immediately after the task is executed
super.onPreExecute();
}

@Override
protected Void doInBackground(Void... params) {

XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element

NodeList nl = doc.getElementsByTagName(KEY_SONG);
// looping through all song nodes <song>
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_ID, parser.getValue(e, KEY_ID));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

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

return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);

}

@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);

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

// Getting adapter by passing xml data ArrayList
adapter = new LazyAdapter(MainActivity.this, songsList);
list.setAdapter(adapter);
}
}

OnCreate方法代码

ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new ShowDialogAsyncTask().execute();

// Click event for single list row
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {

}
});
}

最佳答案

你有——

list.setOnItemClickListener(new OnItemClickListener()

但你从未初始化list

将此行移动到您的 onCreate() --

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

关于android - XML 解析给出 NullPointerException Error with Asynctask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14785097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com