gpt4 book ai didi

java - AsyncTask 返回错误

转载 作者:行者123 更新时间:2023-12-02 06:22:40 24 4
gpt4 key购买 nike

我一直在尝试使用 Asynctask 解析 XML 文件,遵循 [1][2]教程。我在我的 Activity 中实现了一个类,如下所示:

private class GetRoutes extends AsyncTask<String, Void, String[]> {
@Override
protected String[] doInBackground(String... urls) {
String[] read;
try{
RouteReader route = new RouteReader();
read = route.getRoutes();
} catch(IOException iox){
read = new String[1];
read[0] = getResources().getString(R.string.loading_error);
} catch(ArrayIndexOutOfBoundsException aiob){
read = new String[1];
read[0] = getResources().getString(R.string.loading_error);
} catch(NullPointerException npe){
read = new String[1];
read[0] = getResources().getString(R.string.loading_error);
}
return read;
}

@Override
protected void onPostExecute(String[] result) {
values = result;
}
}

然后在我的 onCreate 方法中将其调用为 new GetRoutes().execute("test");。 但是,当我尝试运行此程序时,我的应用程序因 NullPointerException ( logcat is available here ) 而崩溃。

您能指导我如何解决这个问题吗?

<小时/>

为了进一步引用,我的 RouteReader 类如下:

public class RouteReader extends Reader{
public final static String routeURL =
"http://webservices.nextbus.com/service/publicXMLFeed?command=routeList&a=ttc";
private Map<String, String> routes;

public RouteReader()
throws IOException, ArrayIndexOutOfBoundsException{
super(new URL(routeURL));
routes = xmlToMap();
}

public String[] getRoutes(){
return (String[]) routes.keySet().toArray();
}

public String getRouteNum(String route){
return routes.get(route);
}

private Map<String, String> xmlToMap()
throws IOException, ArrayIndexOutOfBoundsException{
Map<String, String> data = new HashMap<String, String>();
String input;
do{
input = getReader().readLine();
if (input.startsWith("<route")){
String[] read = input.split("\"");
data.put(read[3], read[1]);
}
}while (!input.equals("</body>"));
return data;
}

}

最佳答案

来自您的日志:

Caused by: java.lang.NumberFormatException: Invalid int: "1S"

这可能是由这一行引起的:

data.put(read[3], Integer.parseInt(read[1]));

关于java - AsyncTask 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872162/

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