虽然,我使用 AsyncTask 来创建一个用于从服务器调用数据的新线程,但当我转移到此活动时,应用程序仍会崩溃。我无法指定错误的原因。任何帮助??????
public class CustomizedListView extends Activity {
static final String URL = "http://www.Vatrina-eg.com/men_tops.xml";
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_THUMB_URL = "thumb_url";
ListView list;
LazyAdapter adapter;
private class DownloadFilesTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> {
protected ArrayList<HashMap<String, String>> doInBackground(String... strings) {
ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
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_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
// adding HashList to ArrayList
songsList.add(map);
// Getting adapter by passing xml data ArrayList
adapter=new LazyAdapter(CustomizedListView.this, songsList);
list.setAdapter(adapter);
}
return songsList;
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlistview);
list=(ListView)findViewById(R.id.list_listview);
new DownloadFilesTask().execute(URL);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}
}
logcat的:
09-12 22:12:20.906: E/AndroidRuntime(19571): FATAL EXCEPTION: AsyncTask #1
09-12 22:12:20.906: E/AndroidRuntime(19571): Process: com.vatrina.eg, PID: 19571
09-12 22:12:20.906: E/AndroidRuntime(19571): java.lang.RuntimeException: An error occured while executing doInBackground()
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.os.AsyncTask$3.done(AsyncTask.java:300)
09-12 22:12:20.906: E/AndroidRuntime(19571): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
09-12 22:12:20.906: E/AndroidRuntime(19571): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
09-12 22:12:20.906: E/AndroidRuntime(19571): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
09-12 22:12:20.906: E/AndroidRuntime(19571): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
09-12 22:12:20.906: E/AndroidRuntime(19571): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
09-12 22:12:20.906: E/AndroidRuntime(19571): at java.lang.Thread.run(Thread.java:841)
09-12 22:12:20.906: E/AndroidRuntime(19571): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6915)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:3227)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:660)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:660)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:660)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:660)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.view.View.setFlags(View.java:9642)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.view.View.setFocusableInTouchMode(View.java:6343)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.widget.AdapterView.checkFocus(AdapterView.java:777)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.widget.ListView.setAdapter(ListView.java:487)
09-12 22:12:20.906: E/AndroidRuntime(19571): at com.vatrina.eg.CustomizedListView$DownloadFilesTask.doInBackground(CustomizedListView.java:59)
09-12 22:12:20.906: E/AndroidRuntime(19571): at com.vatrina.eg.CustomizedListView$DownloadFilesTask.doInBackground(CustomizedListView.java:1)
09-12 22:12:20.906: E/AndroidRuntime(19571): at android.os.AsyncTask$2.call(AsyncTask.java:288)
09-12 22:12:20.906: E/AndroidRuntime(19571): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
09-12 22:12:20.906: E/AndroidRuntime(19571): ... 4 more
答案 0 :(得分:0)
当您尝试在list
中访问时,onCreate()
很可能仍为NULL。
答案 1 :(得分:0)
将您的onCreate
更改为:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlistview);
list = (ListView)findViewById(R.id.list_listview);
new DownloadFilesTask().execute(URL);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
}
});
}