在某些涉及GSON的Android设备上发生错误

在某些涉及GSON的Android设备上发生错误,android,gson,Android,Gson,这个问题似乎只发生在三星设备上。然而,它确实发生在一个非三星设备上。我一直试图在我的手机上复制这个问题(Nexus5,香草棒棒糖),但没有发生 这将保存BuEntrys的ArrayList的ArrayList private static void saveDataList(ArrayList<ArrayList<ArrayList<BusEntry>>> list, String key1){ SharedPreferences appSharedP

这个问题似乎只发生在三星设备上。然而,它确实发生在一个非三星设备上。我一直试图在我的手机上复制这个问题(Nexus5,香草棒棒糖),但没有发生

这将保存BuEntrys的ArrayList的ArrayList

private static void saveDataList(ArrayList<ArrayList<ArrayList<BusEntry>>> list, String key1){
    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(RO.mainActivity.getApplicationContext());
    SharedPreferences.Editor prefsEditor = appSharedPrefs.edit();
    ArrayList<ArrayList<ArrayList<BusEntry>>> entries = list;
    Gson gson = new Gson();
    String jsonEntries = gson.toJson(entries);
    //Log.d("TAG", "jsonCars = " + jsonEntries);
    prefsEditor.putString(key1, jsonEntries);
    prefsEditor.commit();

}
private static Object getDataList(String key, int task){
    SharedPreferences appSharedPrefs = PreferenceManager
            .getDefaultSharedPreferences(RO.mainActivity.getApplicationContext());
    String jsonString = appSharedPrefs.getString(key, ""); // TODO use empty string as an if statement to determine if you need to recache
    Type type;
    type = new TypeToken<ArrayList<ArrayList<ArrayList<BusEntry>>>>(){}.getType();
    Gson gson = new Gson();
    return gson.fromJson(jsonString, type);
}
你有:

Type type;
type = new TypeToken<ArrayList<ArrayList<ArrayList<BusEntry>>>>(){}.getType();
Gson gson = new Gson();
return gson.fromJson(jsonString, type);
您似乎没有返回预期的类型

我建议如下:

class SpecialArrayType{

    private ArrayList<ArrayList<BusEntry>> specialArrayType;
    public void setSpecialArrayType(ArrayList<ArrayList<BusEntry>> list){
        this.specialArrayType = list;
    }

    public ArrayList<ArrayList<BusEntry>> getSpecialArrayType(){
        return specialArrayType;
    }

}

// Get the array list as a special type, then deal with the nested lists.

ArrayList<SpecialArrayType> lists = new ArrayList<SpecialArrayType>();
Type type = new TypeToken<ArrayList<SpecialArrayType>>(){}.getType();
Gson gson = new Gson();
lists = gson.fromJson(jsonString, type);

ArrayList<ArrayList<ArrayList<BusEntry>>> returnList = new ArrayList<ArrayList<ArrayList<BusEntry>>>();

// Loop through special array list to populate info from nested array lists.
for(SpecialArrayType list1: lists){

    // We have these items in our outer array list, we want to add them to our
    // return list.
    returnList.Add(list1);

    // The next nested array list.
    list1 = new ArrayList<ArrayList<BusEntry>>();

    for(ArrayList<BusEntry> list2: list1){
        // Populate each specialArrayType with an array list of array list of BusEntries.
        list1.Add(list2);
        // Find each nested array list of bus entries.
        list2= new ArrayList<BusEntry>> ();

        for(BusEntry entry: list2){
            list2.Add(entry);
        }
    }

}       

return returnList;
class specialraytype{
专用阵列列表专用阵列类型;
public void setSpecialArrayType(ArrayList列表){
this.specialrayType=列表;
}
公共阵列列表getSpecialArrayType(){
返回特殊射线类型;
}
}
//将数组列表作为特殊类型获取,然后处理嵌套列表。
ArrayList list=新的ArrayList();
Type Type=new-TypeToken(){}.getType();
Gson Gson=新的Gson();
lists=gson.fromJson(jsonString,type);
ArrayList returnList=新建ArrayList();
//循环特殊数组列表以填充嵌套数组列表中的信息。
对于(特殊光线类型列表1:列表){
//我们的外部数组列表中有这些项,我们希望将它们添加到
//返回列表。
返回列表。添加(列表1);
//下一个嵌套数组列表。
list1=新的ArrayList();
对于(ArrayList list2:list1){
//用BusEntries数组列表的数组列表填充每个SpecialAryType。
列表1.添加(列表2);
//查找总线项的每个嵌套数组列表。
list2=新数组列表>();
用于(总线条目:列表2){
列表2.添加(条目);
}
}
}       
退货清单;
因此,您将返回完全嵌套的数据。

您有:

Type type;
type = new TypeToken<ArrayList<ArrayList<ArrayList<BusEntry>>>>(){}.getType();
Gson gson = new Gson();
return gson.fromJson(jsonString, type);
您似乎没有返回预期的类型

我建议如下:

class SpecialArrayType{

    private ArrayList<ArrayList<BusEntry>> specialArrayType;
    public void setSpecialArrayType(ArrayList<ArrayList<BusEntry>> list){
        this.specialArrayType = list;
    }

    public ArrayList<ArrayList<BusEntry>> getSpecialArrayType(){
        return specialArrayType;
    }

}

// Get the array list as a special type, then deal with the nested lists.

ArrayList<SpecialArrayType> lists = new ArrayList<SpecialArrayType>();
Type type = new TypeToken<ArrayList<SpecialArrayType>>(){}.getType();
Gson gson = new Gson();
lists = gson.fromJson(jsonString, type);

ArrayList<ArrayList<ArrayList<BusEntry>>> returnList = new ArrayList<ArrayList<ArrayList<BusEntry>>>();

// Loop through special array list to populate info from nested array lists.
for(SpecialArrayType list1: lists){

    // We have these items in our outer array list, we want to add them to our
    // return list.
    returnList.Add(list1);

    // The next nested array list.
    list1 = new ArrayList<ArrayList<BusEntry>>();

    for(ArrayList<BusEntry> list2: list1){
        // Populate each specialArrayType with an array list of array list of BusEntries.
        list1.Add(list2);
        // Find each nested array list of bus entries.
        list2= new ArrayList<BusEntry>> ();

        for(BusEntry entry: list2){
            list2.Add(entry);
        }
    }

}       

return returnList;
class specialraytype{
专用阵列列表专用阵列类型;
public void setSpecialArrayType(ArrayList列表){
this.specialrayType=列表;
}
公共阵列列表getSpecialArrayType(){
返回特殊射线类型;
}
}
//将数组列表作为特殊类型获取,然后处理嵌套列表。
ArrayList list=新的ArrayList();
Type Type=new-TypeToken(){}.getType();
Gson Gson=新的Gson();
lists=gson.fromJson(jsonString,type);
ArrayList returnList=新建ArrayList();
//循环特殊数组列表以填充嵌套数组列表中的信息。
对于(特殊光线类型列表1:列表){
//我们的外部数组列表中有这些项,我们希望将它们添加到
//返回列表。
返回列表。添加(列表1);
//下一个嵌套数组列表。
list1=新的ArrayList();
对于(ArrayList list2:list1){
//用BusEntries数组列表的数组列表填充每个SpecialAryType。
列表1.添加(列表2);
//查找总线项的每个嵌套数组列表。
list2=新数组列表>();
用于(总线条目:列表2){
列表2.添加(条目);
}
}
}       
退货清单;

因此,您将返回完全嵌套的数据。

在android studio项目中添加以下库,而不是将手动库放在库文件夹中

 compile 'com.google.code.gson:gson:2.3.1'

如果您试图使用GSON在SharedReferences中存储arraylist或class对象,请尝试本教程:

在android studio项目中添加以下库,而不是将手动库放在库文件夹中

 compile 'com.google.code.gson:gson:2.3.1'

如果您试图使用GSON将arraylist或class对象存储在SharedReferences中,请尝试本教程:

您可以发布如何调用DataStorage.saveDataList()、DataStorage.getAllData()的代码吗?这种情况是否发生在多个三星设备上?nexus和三星的Android o.s.版本是什么?@user2450263这在多台三星设备以及HTC设备上都有发生。我还没有在我的Nexus 5上实现。你能发布你如何调用DataStorage.saveDataList()、DataStorage.getAllData()的代码吗?它在多个三星设备上实现了吗?nexus和三星的Android o.s.版本是什么?@user2450263这在多台三星设备以及HTC设备上都有发生。我还没有让它发生在我的Nexus5上。嘿,谢谢你的回复。我还不是100%确定,但我相信问题其实并不存在于我的代码中。我认为问题在于我最初将json存储为数组,然后在新版本中将其更改为ArrayList。因此,当人们升级到新版本的应用程序时,它期望ArrayList,而存储的json是Arrays。无论如何,我会给你50分,因为你说的话让我意识到了这个问题。谢谢嘿,谢谢你的回复。我还不是100%确定,但我相信问题其实并不存在于我的代码中。我认为问题在于我最初将json存储为数组,然后在新版本中将其更改为ArrayList。因此,当人们升级到新版本的应用程序时,它期望ArrayList,而存储的json是Arrays。无论如何,我会给你50分,因为你说的话让我意识到了这个问题。谢谢