当前位置:网站首页>SharedPreferences 保存List<Bean> 到本地并解决com.google.gson.internal.LinkedTreeMap cannot be cast to异常
SharedPreferences 保存List<Bean> 到本地并解决com.google.gson.internal.LinkedTreeMap cannot be cast to异常
2022-07-02 22:10:00 【怀化纱厂科比】
最近有阵子没写博客了,跳槽了,转了个方向,是我之前一直想去的领域,我也准备了好一阵子,找这个方向的工作,运气不错,遇到了好的老大,这几天计划了下,今年后续计划,从2方面出发,一方面记录日常遇到问题,一方面梳理源码
今天帖子内容说下遇到的,大概说下场景,需要讲一个List <Bean > 保存在本地用SP,网上搜到很多帖子,本来是想找个不用GSON的,但是实在是没找到,应为是在SystemUI层改,后面没办法,我在 android.bp 文件里面加入了
java_import {
name: "core-settings",
jars: ["libs/core.jar"],
sdk_version: "current",
}
继续说,网上找到的方法类
public class ListDataSave {
private SharedPreferences preferences;
private SharedPreferences.Editor editor;
public ListDataSave(Context mContext) {
preferences = mContext.getSharedPreferences("stock_info", Context.MODE_PRIVATE);
editor = preferences.edit();
}
/**
* 保存List
* @param tag
* @param datalist
*/
public <T> void setDataList(String tag, List<T> datalist) {
if (null == datalist || datalist.size() <= 0)
return;
Gson gson = new Gson();
//转换成json数据,再保存
String strJson = gson.toJson(datalist);
editor.clear();
editor.putString(tag, strJson);
editor.commit();
}
/**
* 获取List
* @param tag
* @return
*/
public <T> List<T> getDataList(String tag) {
List<T> datalist=new ArrayList<T>();
String strJson = preferences.getString(tag, null);
if (null == strJson) {
return datalist;
}
Gson gson = new Gson();
datalist = gson.fromJson(strJson, new TypeToken<List<T>>() {
}.getType());
return datalist;
}
}
问题点:
运行这段代码后,我发现出了异常:
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to
然后我断点看了发下,抛出这种异常的原因就是因为取出的时候使用Gson解析Json数据过程中,运行充抛出这样的问题异常的,简单来说就是在类中带有泛型时,gson无法识别其中的泛型而转成了LinkenTreeMap的类型。
解决办法就是:在使用泛型过程中,必须要具体到某一个类,不用使用泛型代替。
吧上面的T 改成你要用的bean 就能解决
边栏推荐
- Brief introduction of emotional dialogue recognition and generation
- Antd component upload uploads xlsx files and reads the contents of the files
- Jerry's prototype will trigger shutdown after multiple touches [chapter]
- STM32之ADC
- mysql重置密码,忘记密码,重置root密码,重置mysql密码
- Alibaba cloud award winning experience: how to use polardb-x
- 解决Chrome浏览器和Edeg浏览器主页被篡改的方法
- Splunk audit setting
- Go language sqlx library operation SQLite3 database addition, deletion, modification and query
- PotPlayer设置最小化的快捷键
猜你喜欢
Set right click to select vs code to open the file
首批 | 腾讯云完成国内首个云原生安全成熟度评估
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
Lambda表达式:一篇文章带你通透
Go语言sqlx库操作SQLite3数据库增删改查
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
設置單擊右鍵可以選擇用VS Code打開文件
[Solved] Splunk: Cannot get username when all users are selected“
Qt QSplitter拆分器
Xshell configuration xforward forwarding Firefox browser
随机推荐
SQL进阶语法
Cryptographic technology -- key and ssl/tls
SQL advanced syntax
Qt QProgressBar详解
潘多拉 IOT 开发板学习(HAL 库)—— 实验3 按键输入实验(学习笔记)
Doorplate making C language
Cryptography -- the mode of block cipher
Methods to solve the tampering of Chrome browser and edeg browser homepage
20220527_ Database process_ Statement retention
Alibaba cloud award winning experience: how to use polardb-x
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
泛型与反射,看这篇就够了
Qt QScrollArea
创新实力再获认可!腾讯安全MSS获2022年度云原生安全守护先锋
编辑卡顿
从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
力扣刷题(2022-6-28)
数据标注典型案例,景联文科技如何助力企业搭建数据方案
Solving ordinary differential equations with MATLAB
密码技术---分组密码的模式