【发布时间】:2015-07-28 06:32:28
【问题描述】:
这是发生崩溃的那一行
offsetDuration = duration - (offsets.containsKey(freq)
? offsets.get(freq) : 0l);
我通过捕获Exception 并转储变量得到的值,
long offsetDuration = 0;
long duration = 391144;
TreeMap<Long, Long> offsets = {0=4024974.0, 1036800=8588.0, 1190400=88216.0, 1267200=49763.0, 1497600=87476.0, 1574400=7469.0, 1728000=54553.0, 1958400=60512.0, 2265600=246942.0, 300000=390779.0, 422400=39945.0, 652800=55204.0, 729600=46829.0, 883200=19191.0, 960000=23888.0}
long freq = 300000;
变量TreeMap<Long, Long> offsets是使用下面的代码从一个json文件中解析出来的。
@NonNull
public static TreeMap<Long, Long> getOffsets(Context context) throws CpuStateException {
File file = getOffsetsFile(context);
TreeMap<Long, Long> map;
try {
String s = Files.toString(file, Charsets.UTF_8).trim();
Gson gson = new GsonBuilder().create();
Type type = new TypeToken<TreeMap<Long, Long>>(){}.getType();
map = gson.fromJson(s, type);
} catch (IOException e) {
throw new CpuStateException("Failed to read offsets!");
}
if (map == null)
throw new CpuStateException("Failed to read offsets!");
return map;
}
多次检查代码后,我无法确定这段代码可以抛出的情况
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Long
at java.lang.Long.compareTo(Long.java:32)
at java.util.TreeMap.find(TreeMap.java:277)
at java.util.TreeMap.findByObject(TreeMap.java:351)
at java.util.TreeMap.containsKey(TreeMap.java:182)
at com.vibhinna.library.engine.CpuStates.getCpuData(CpuStates.java:96)
at com.vibhinna.library.engine.CpuStates.getBarData(CpuStates.java:162)
有什么想法吗?
更新 1:这是生成 json 的方式:
public static void offsetTimers(TreeMap<Long, Long> offsets, Context context) throws CpuStateException {
Gson gson = new GsonBuilder().create();
String json = gson.toJson(offsets);
File file = getOffsetsFile(context);
try {
OutputStreamWriter outputStream = new OutputStreamWriter(new FileOutputStream(file),
"UTF-8");
outputStream.write(json);
outputStream.flush();
outputStream.close();
} catch (IOException e) {
throw new CpuStateException("Failed to save offsets!");
}
}
更新 2 :即使在原始应用程序中,我什至无法重现此问题,崩溃很少见,只有不到 1% 的客户报告。
更新 3
offset class: class java.lang.String value: 1036800
offset class: class java.lang.String value: 1190400
offset class: class java.lang.String value: 1267200
offset class: class java.lang.String value: 1497600
offset class: class java.lang.String value: 1574400
offset class: class java.lang.String value: 1728000
offset class: class java.lang.String value: 1958400
offset class: class java.lang.String value: 2265600
offset class: class java.lang.String value: 300000
offset class: class java.lang.String value: 422400
offset class: class java.lang.String value: 652800
offset class: class java.lang.String value: 729600
offset class: class java.lang.String value: 883200
offset class: class java.lang.String value: 960000
json:
{
"0":256093,
"300000":105045,
"422400":9677,
"652800":10443,
"729600":8868,
"883200":3951,
"960000":7323,
"1036800":18668,
"1190400":34938,
"1267200":17151,
"1497600":11018,
"1574400":1173,
"1728000":22881,
"1958400":21076,
"2265600":66501
}
【问题讨论】:
-
请发布原始 JSON...您能否在一个简短但完整的桌面应用程序中重现此内容?
-
@JonSkeet 我什至无法在原始应用程序中重现此问题,崩溃很少见,只有不到 1% 的客户报告。
-
那么值得将这些信息放在问题中。我认为这意味着您不知道导致此问题的 JSON?
-
这就是 JSON 的 一个 示例。只是为了尝试重现这一点,请尝试指定一个真正 huge
long值,该值不能完全表示为double... 9223372036854775806 就可以了。查看生成了什么 JSON,以及解析它时会发生什么。 -
堆栈跟踪表明 java.lang.String 对象正在从
CpuStates.getCpuData(CpuStates.java:96)向下传递。我认为您需要找出该对象的来源。
标签: java android json gson classcastexception