我在使用 Android Q 中的 hashmap 的 createFromParcel 中也面临同样的问题,通过迭代 hashmap 并将值转换为其各自的类型来解决它。
private ContentValues getContentValuesFromHashMapValues(HashMap<String, Object> hashMap) {
ContentValues contentValues = new ContentValues();
for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
Object value = entry.getValue();
String key = entry.getKey();
if (value instanceof Integer) {
contentValues.put(key, (Integer) value);
} else if (value instanceof Long) {
contentValues.put(key, (Long) value);
} else if (value instanceof Short) {
contentValues.put(key, (Short) value);
} else if (value instanceof Float) {
contentValues.put(key, (Float) value);
} else if (value instanceof Double) {
contentValues.put(key, (Double) value);
} else if (value instanceof Byte) {
contentValues.put(key, (Byte) value);
} else if (value instanceof Boolean) {
contentValues.put(key, (Boolean) value);
} else if (value instanceof String) {
contentValues.put(key, ((value == null) ? "" : (String) value));
}
}
return contentValues;
}