雖然我使用AsyncTask來創建一個用於從服務器調用數據的新線程,但是當我移動到此活動時,應用程序仍然崩潰。我無法指定錯誤的原因。任何幫助??????Android:無法啓動活動
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
你永遠不會初始化'list'。 – dharms 2014-09-12 18:50:49
我初始化了它,但沒有改變。 'ListView list =(ListView)findViewById(R.id.list_listview);' – 2014-09-12 18:57:20
將其更改爲'list =(ListView)findViewById(R.id.list_listview);'。在前面添加ListView使它成爲一個局部變量。 – dharms 2014-09-12 18:58:44