当前位置:网站首页>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 就能解决
边栏推荐
- Qt QProgressBar详解
- Cryptography -- the mode of block cipher
- Chow-Liu Tree
- Realize the linkage between bottomnavigationview and navigation
- 实现BottomNavigationView和Navigation联动
- FOC矢量控制及BLDC控制中的端电压、相电压、线电压等概念别还傻傻分不清楚
- Methods to solve the tampering of Chrome browser and edeg browser homepage
- pytorch训练CPU占用持续增长(bug)
- 海思3559万能平台搭建:在截获的YUV图像上画框
- 移动端 1px 像素兼容性问题,实现1px 边框
猜你喜欢

程序员版本的八荣八耻~

Redis expiration policy +conf record

Introduction to the latest plan of horizon in April 2022

PotPlayer设置最小化的快捷键

AES高级加密协议的动机阐述

QT qpprogressbar details

WebRTC音视频采集和播放示例及MediaStream媒体流解析

QT qsplitter splitter

Generics and reflection, this is enough

4 special cases! Schools in area a adopt the re examination score line in area B!
随机推荐
The first batch of Tencent cloud completed the first cloud native security maturity assessment in China
Jerry's fast touch does not respond [chapter]
Qt QScrollArea
[Yangcheng cup 2020] easyphp
情感对话识别与生成简述
WebRTC音视频采集和播放示例及MediaStream媒体流解析
20220524_数据库过程_语句留档
Mask R-CNN
Go语言sqlx库操作SQLite3数据库增删改查
Jerry's prototype has no touch, and the reinstallation becomes normal after dismantling [chapter]
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
Use the scroll bar of souI when using the real window in souI
Jerry's prototype will trigger shutdown after multiple touches [chapter]
Splunk audit setting
从底层结构开始学习FPGA----Xilinx ROM IP的定制与测试
[hardware] origin of standard resistance value
最小生成树 Minimum Spanning Tree
Numerical solution of partial differential equations with MATLAB
Easyclick, EC Quanlang network verification source code
Antd component upload uploads xlsx files and reads the contents of the files