Java Can';t从HashMap中检索Double<;整数,双精度>;

Java Can';t从HashMap中检索Double<;整数,双精度>;,java,android,hashmap,gson,Java,Android,Hashmap,Gson,不知何故,我无法从使用Gson制作的哈希映射中检索到Double Map<Integer, Double> ratingMap = (Map<Integer, Double>) new GsonBuilder() .create().fromJson(json, Map.class); Integer ifilmId = filmId; Double rating = ratingMap.get(ifilmId); 我让Gson将整数格式化为Do

不知何故,我无法从使用Gson制作的哈希映射中检索到Double

Map<Integer, Double> ratingMap = (Map<Integer, Double>) new GsonBuilder()
            .create().fromJson(json, Map.class);
Integer ifilmId = filmId;
Double rating = ratingMap.get(ifilmId);
我让Gson将整数格式化为Double,这似乎很好,但我无法检索它

总代码,包括保存到Androids SharedReferences的代码

public void saveRating(int rating, int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(
            LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    Map<Integer, Integer> ratingMap;
    if (json.equals("")) {
        // noting ever saved
        ratingMap = new HashMap<Integer, Integer>();
        ratingMap.put(filmId, rating);

    } else {
        ratingMap = (Map<Integer, Integer>) new GsonBuilder().create()
                .fromJson(json, Map.class);
        ratingMap.put(Integer.valueOf(filmId), rating);
    }

    json = new GsonBuilder().create().toJson(ratingMap, Map.class);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(LOCAL_MAP_RATING_KEY, json);
    editor.commit();
}

/*
 * returns null if no rating found
 */
public Map<Integer, Integer> getRatingFor(int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(
            LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    if (json.equals("")) {
        return null;
    }

    Map<Integer, Integer> ratingMap = (Map<Integer, Integer>) new GsonBuilder()
            .create().fromJson(json, Map.class);
    Log.d("map", ratingMap.toString());
    Integer ifilmId = filmId;
    Integer rating = ratingMap.get(ifilmId);
    if(rating == null) { //because of this we have to prevent a 0 rating
        return null;
    } else {
        Map<Integer, Integer> returnMap = new HashMap<Integer, Integer>();
        returnMap.put(filmId, rating.intValue());
        return returnMap;
    }
}
public void saveRating(int rating,int filmId){
SharedReferences SharedReferences=context.getSharedReferences(
本地记忆键,0);
String json=SharedRef.getString(本地映射、评级、键“”);
地图分级图;
if(json.equals(“”){
//什么也救不了
ratingMap=新HashMap();
ratingMap.put(电影ID、评级);
}否则{
ratingMap=(映射)新建GsonBuilder().create()
.fromJson(json,Map.class);
ratingMap.put(整数.valueOf(filmId),额定值);
}
json=new GsonBuilder().create().toJson(ratingMap,Map.class);
SharedPreferences.Editor=sharedPref.edit();
putString(LOCAL\u MAP\u RATING\u KEY,json);
commit();
}
/*
*如果未找到评级,则返回null
*/
公共地图getRatingFor(int filmId){
SharedReferences SharedReferences=context.getSharedReferences(
本地记忆键,0);
String json=SharedRef.getString(本地映射、评级、键“”);
if(json.equals(“”){
返回null;
}
地图等级地图=(地图)新的GsonBuilder()
.create().fromJson(json,Map.class);
Log.d(“map”,ratingMap.toString());
整数ifilmId=胶片ID;
整数评级=ratingMap.get(ifilmId);
如果(rating==null){//因此,我们必须防止评级为0
返回null;
}否则{
Map returnMap=newhashmap();
returnMap.put(filmId,rating.intValue());
返回地图;
}
}
公共无效保存评级(双重评级,int-filmId){
SharedReferences SharedReferences=context.getSharedReferences(LOCAL_MEM_键,0);
String json=SharedRef.getString(本地映射、评级、键“”);
地图分级图;
if(json.equals(“”){
//什么也救不了
ratingMap=新HashMap();
}否则{
ratingMap=(Map)new GsonBuilder().create().fromJson(json,Map.class);
}
ratingMap.put(电影ID、评级);
ratingMap.put(3,5.0d);//仅用于测试
json=new GsonBuilder().create().toJson(ratingMap,Map.class);
SharedPreferences.Editor=sharedPref.edit();
putString(LOCAL\u MAP\u RATING\u KEY,json);
commit();
}
/*
*如果未找到评级,则返回null
*/
公共地图getRatingFor(int filmId){
SharedReferences SharedReferences=context.getSharedReferences(LOCAL_MEM_键,0);
String json=SharedRef.getString(本地映射、评级、键“”);
if(json.equals(“”){
返回null;
}
Map ratingMap=(Map)new GsonBuilder().create().fromJson(json,Map.class);
Log.d(“map”,ratingMap.toString());
Log.d(“map”,ratingMap.get(3)+;//仅用于测试
整数ifilmId=胶片ID;
双重评级=ratingMap.get(ifilmId);
如果(rating==null){//因此,我们必须防止评级为0
返回null;
}否则{
Map returnMap=newhashmap();
returnMap.put(filmId,评级);
返回地图;
}
}

确保保存时未传递空变量

saveRating(int rating, int filmId){
    Log.d(TAG, String.valueOf(rating));
}

if(json.equals(“”){
//什么也救不了

ratingMap=new HashMap();试着做一个
map。把(2,4.0)
,然后打印
map
。看看你得到了什么。你创建了一个
map
,然后你试着得到一个
map
?@AlexisC。是的。Gson确实正确格式化了。当我把检索到的map改成“map”时,问题仍然存在。你说
Log.d(“map”),ratingMap.toString();
打印
{2=5.0}
,并且
ifilmId
为2,您得到
null
?您可以试着发布一个小程序来演示这个问题吗?我认为它与
共享引用
无关,所以一个简单的Java程序和您试图保存的JSON是可以的。“您可以在HashMap中变异对象。禁止这样做。”“没有。你从哪里得到的?很抱歉,我没有得到答案。我在哪里变异哈希映射?但我只是在调用。为一个唯一的键放置一次。然后我调用。为另一个键放置一次。@TimKranen给我一分钟:)我已经尝试过了。问题仍然存在。字符串映射打印:{2=5.0},当使用.get(filmId)时当filmId=2时,作为double返回的评级为null。我尝试使用double.valueOf(评级)。但它不起作用。检索到的评级一直返回null,而我的logcat指示{2=4.0}
public void saveRating(Double rating, int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");
    Map<Integer, Double> ratingMap;
    if (json.equals("")) {
        // noting ever saved
        ratingMap = new HashMap<Integer, Double>();
    } else {
        ratingMap = (Map<Integer, Double>) new GsonBuilder().create().fromJson(json, Map.class);
    }
    ratingMap.put(filmId, rating);
    ratingMap.put(3, 5.0d); // JUST FOR TEST
    json = new GsonBuilder().create().toJson(ratingMap, Map.class);
    SharedPreferences.Editor editor = sharedPref.edit();

    editor.putString(LOCAL_MAP_RATING_KEY, json);
    editor.commit();
}

/*
 * returns null if no rating found
 */
public Map<Integer, Double> getRatingFor(int filmId) {
    SharedPreferences sharedPref = context.getSharedPreferences(LOCAL_MEM_KEY, 0);
    String json = sharedPref.getString(LOCAL_MAP_RATING_KEY, "");

    if (json.equals("")) {
        return null;
    }

    Map<Integer, Double> ratingMap = (Map<Integer, Double>) new GsonBuilder().create().fromJson(json, Map.class);
    Log.d("map", ratingMap.toString());
    Log.d("map", ratingMap.get(3) + ""); // JUST FOR TEST
    Integer ifilmId = filmId;
    Double rating = ratingMap.get(ifilmId);

    if (rating == null) { //because of this we have to prevent a 0 rating
        return null;
    } else {
        Map<Integer, Double> returnMap = new HashMap<Integer, Double>();
        returnMap.put(filmId, rating);
        return returnMap;
    }
}
saveRating(int rating, int filmId){
    Log.d(TAG, String.valueOf(rating));
}
if (json.equals("")) {
    // noting ever saved
    ratingMap = new HashMap<Integer, Double>();    <--- Double not Integer
    ratingMap.put(filmId, 5.0);

} else {
    ratingMap = (Map<Integer, Double>) new GsonBuilder().create()
            .fromJson(json, Map.class);           <--- double not Integer
    ratingMap.put(Integer.valueOf(filmId), 5.0);
}