当前位置:网站首页>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 就能解决
边栏推荐
- 20220527_ Database process_ Statement retention
- [Yangcheng cup 2020] easyphp
- Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - packaging based on thinkphp5 - detailed guidance of the author - July 1, 2022 08:43:
- 20220524_数据库过程_语句留档
- [羊城杯2020]easyphp
- Niuke network: maximum submatrix
- 泛型与反射,看这篇就够了
- 聊聊内存模型与内存序
- 编辑卡顿
- 20220527_数据库过程_语句留档
猜你喜欢
Start from the bottom structure to learn the customization and testing of FPGA --- Xilinx ROM IP
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
力扣刷题(2022-6-28)
Brief introduction of emotional dialogue recognition and generation
Application of containerization technology in embedded field
4 special cases! Schools in area a adopt the re examination score line in area B!
Li Kou brush questions (2022-6-28)
Looking at Ctrip's toughness and vision from the Q1 financial report in 2022
mysql重置密码,忘记密码,重置root密码,重置mysql密码
FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
随机推荐
【硬件】标准阻值的由来
PMP project integration management
Mask R-CNN
聊聊内存模型与内存序
情感对话识别与生成简述
Redis 过期策略+conf 记录
WebRTC音视频采集和播放示例及MediaStream媒体流解析
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - packaging based on thinkphp5 - detailed guidance of the author - July 1, 2022 08:43:
海思3559万能平台搭建:在截获的YUV图像上旋转操作
AES高級加密協議的動機闡述
【喜欢的诗词】好了歌
海思 VI接入视频流程
Realize the linkage between bottomnavigationview and navigation
Data analysis learning records -- complete a simple one-way ANOVA with Excel
Go language sqlx library operation SQLite3 database addition, deletion, modification and query
Qt QSplitter拆分器
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
Analyse des données dossiers d'apprentissage - - analyse simple de la variance à facteur unique avec Excel
实现BottomNavigationView和Navigation联动
Li Kou brush questions (2022-6-28)