I encountered a problem when I tried to run the following code:
When I trying to get item from ArrayList<double[]> list
but it throws ClassCastException
Method list.get(i)
also throws excexption
public void drawRoutes(ArrayList<String> routes) {
if (routes.isEmpty()) return;
for (int i = 0; i < routes.size(); i++) {
PolylineOptions polylineOptions = new PolylineOptions();
ArrayList<double[]> list = TransportRoutes.getRoutes(tag, routes).get(i);
for (double[] points : list) { // throws ClassCastException
map.addPolyline(polylineOptions
.add(new LatLng(points[0], points[1]))
.color(color)
.width(POLY_LINE_WIDTH));
}
}
}
method getRoutes() :
public static ArrayList<ArrayList<double[]>> getRoutes(String tag, ArrayList<String> numbers) {
ArrayList<ArrayList<double[]>> routes = new ArrayList<>();
for (String number : numbers) {
routes.add(sRoutesHashMap.get(tag).get(number));
}
return routes;
}
// sRoutesHashMap is a HashMap<String, HashMap<String, ArrayList<double[]>>>
// and taken from this method
protected static <T> T getSmth(Context context, String url) {
T data = null;
JSONParser jsonParser = new JSONParser(context);
String json;
ObjectMapper mapper = new ObjectMapper();
try {
json = jsonParser.execute(url).get();
if (json != null) {
data = mapper.readValue(json, new TypeReference<T>() {});
} else return null;
} catch (InterruptedException | ExecutionException | IOException e) {
e.printStackTrace();
}
return data;
}
Full stacktrace is:
java.lang.ClassCastException: java.util.ArrayList cannot be cast to double[]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to double[]
at in.transee.transee.Drawer.drawRoutes(Drawer.java:46)
at in.transee.transee.MapsActivity.onCreate(MapsActivity.java:45)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
When I used this method everythink worked:
public static void getAllRoutes(Context context) {
String routesJson = sSPData.getString(sCity + ROUTES, EMPTY);
ObjectMapper mapper = new ObjectMapper();
JSONParser jsonParser = new JSONParser();
try {
routesJson = jsonParser.execute(URL + sCity + SEPARATOR + ROUTES).get().get(0);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
try {
sRoutesHashMap = mapper.readValue(routesJson,
new TypeReference<HashMap<String, HashMap<String, ArrayList<double[]>>>>() {});
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectMapper
from? – Andy Brown Feb 20 at 20:07