出于某种原因,这段代码抛出了一个ClassCastException
,告诉我我不能将 Double 转换为 Float。异常来自下面的第一行代码。mapData.speeds
是一个ArrayList<Float>
。双子在哪里?
float spd = mapData.speeds.get(focusPointIndex);
spd = (spd * 3600/1609.34);
这是完整的堆栈跟踪:
03-31 01:00:51.008: E/AndroidRuntime(17778): FATAL EXCEPTION: main
03-31 01:00:51.008: E/AndroidRuntime(17778): java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Float
03-31 01:00:51.008: E/AndroidRuntime(17778): at net.taptools.android.trailtracker.ResultsMapViewingFragment$4.onMapLongClick(ResultsMapViewingFragment.java:224)
03-31 01:00:51.008: E/AndroidRuntime(17778): at com.google.android.gms.maps.GoogleMap$5.onMapLongClick(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at com.google.android.gms.internal.k$a.onTransact(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at android.os.Binder.transact(Binder.java:326)
03-31 01:00:51.008: E/AndroidRuntime(17778): at com.google.android.gms.maps.internal.IOnMapLongClickListener$Stub$Proxy.onMapLongClick(IOnMapLongClickListener.java:93)
03-31 01:00:51.008: E/AndroidRuntime(17778): at maps.i.s.a(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at maps.y.v.d(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at maps.y.bf.onLongPress(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at maps.d.v.onLongPress(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at maps.d.h.c(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at maps.d.h.c(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at maps.d.j.handleMessage(Unknown Source)
03-31 01:00:51.008: E/AndroidRuntime(17778): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 01:00:51.008: E/AndroidRuntime(17778): at android.os.Looper.loop(Looper.java:137)
03-31 01:00:51.008: E/AndroidRuntime(17778): at android.app.ActivityThread.main(ActivityThread.java:5059)
03-31 01:00:51.008: E/AndroidRuntime(17778): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 01:00:51.008: E/AndroidRuntime(17778): at java.lang.reflect.Method.invoke(Method.java:511)
03-31 01:00:51.008: E/AndroidRuntime(17778): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
03-31 01:00:51.008: E/AndroidRuntime(17778): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
03-31 01:00:51.008: E/AndroidRuntime(17778): at dalvik.system.NativeStart.main(Native Method)
- 编辑 -
我想我已经找到了导致错误的原因,但仍然不知道它为什么会发生,或者如何解决它。我正在从 JSON 中解析 mapData 对象,并且因为它包含许多各种类型的 ArrayList,我创建了一个将 JSONArray 解析为指定类型的 ArrayList 的方法。这是方法。
private <T> ArrayList<T> JSONArrayToList(JSONArray jsonArr){
ArrayList<T> arrList = new ArrayList<T>();
for(int i = 0; i<jsonArr.length(); i++){
try {
arrList.add((T)jsonArr.get(i));
} catch (JSONException e){e.printStackTrace();}
}
return arrList;
}