我尝试从Url解析xml文件并在文件中获取Lat值,然后将它们显示在Toast窗口中以进行调试。我收到此错误:"无法在未调用Looper.prepare()"
的线程内创建处理程序这是我目前正在使用的代码:
public void getGpx(String area) {
final String area2 = area;
new GHAsyncTask<Void, Void, List<String>>() {
protected List<String> saveDoInBackground(Void... params)
throws Exception {
String urlString = fileListURL + area2 + ".gpx";
logUser(urlString);
ArrayList testingXml = null;
try {
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
NodeList nodes = doc.getElementsByTagName("trkpt");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
NodeList title = element.getElementsByTagName("lat");
Element line = (Element) title.item(0);
testingXml.add(line.getTextContent());
}
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
} catch (ParserConfigurationException a) {
}
return testingXml;
}
@Override
protected void onPostExecute(List<String> gpxList) {
if (hasError()) {
getError().printStackTrace();
logUser("Are you connected to the internet? Problem while fetching remote area list: "
+ getErrorMessage());
return;
} else if (gpxList == null || gpxList.isEmpty()) {
logUser("something went wrong");
return;
}
logUser(gpxList.toString());
}
}.execute();
}
我希望有人可以帮我解决这个错误。代码中可能还有其他错误,我还没有找到。只是一个新手试图拼凑这个。
答案 0 :(得分:0)
你不能在异步任务的UI线程中做任何事情,可能你的asynctask内部的一些函数正在调用UI线程,你可以调用它们onPreExecute,onPostExecute,或者在asynctask运行时使用publishProgress来调用UI线程。