当前位置:网站首页>SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
SharedPreferences save list < bean > to local and solve com google. gson. internal. Linkedtreemap cannot be cast to exception
2022-07-02 23:19:00 【Huaihua yarn factory Kobe】
I haven't blogged recently , Job hopping. , Turned a direction , It's the field I've always wanted to go to , I also prepared for a while , Find a job in this direction , Good luck. , Met a good boss , I have made plans for these days , Follow up plan for this year , from 2 Aspect departure , On the one hand, record daily problems , On the one hand, sort out the source code
Today's post says what I met , About the scene , We need to talk about one List <Bean > Save locally SP, I found many posts on the Internet , Originally, I wanted to find someone who didn't use GSON Of , But I really didn't find , It should be in SystemUI Layer modification , There's no way back , I am here android.bp It's added to the file
java_import {
name: "core-settings",
jars: ["libs/core.jar"],
sdk_version: "current",
}
Continue to say , Methods found online
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();
}
/**
* preservation 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();
// convert to json data , Save again
String strJson = gson.toJson(datalist);
editor.clear();
editor.putString(tag, strJson);
editor.commit();
}
/**
* obtain 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;
}
}
The problem is :
After running this code , I found something unusual :
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to
Then I read it and sent it , The reason why this exception is thrown is because it is used when fetching Gson analysis Json In the data process , Running charge throws such a problem exception , Simply put, it is to have Generic when ,gson Unable to recognize the generics, it turned into LinkenTreeMap The type of .
The solution is : In the process of using generics , It must be specific to a certain class , Instead of using generics .
Let's go up there T Change it to what you want bean It can be solved
边栏推荐
- (毒刺)利用Pystinger Socks4上线不出网主机
- [redis notes] compressed list (ziplist)
- 高数有多难?AI 卷到数学圈,高数考试正确率 81%!
- Win11系统explorer频繁卡死无响应的三种解决方法
- 【Proteus仿真】51单片机+LCD12864推箱子游戏
- Go basic anonymous variable
- Cryptographic technology -- key and ssl/tls
- Cryptography -- the mode of block cipher
- Solving ordinary differential equations with MATLAB
- 【Redis笔记】压缩列表(ziplist)
猜你喜欢

The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear

Construction of Hisilicon 3559 universal platform: draw a frame on the captured YUV image

情感对话识别与生成简述

STM32之ADC
![Eight bit responder [51 single chip microcomputer]](/img/4f/683948fca93a0dc1a0409839f60759.jpg)
Eight bit responder [51 single chip microcomputer]

Talk about memory model and memory order

Minimum spanning tree

【STL源码剖析】仿函数(待补充)

The first batch of Tencent cloud completed the first cloud native security maturity assessment in China

Use of recyclerview with viewbinding
随机推荐
(毒刺)利用Pystinger Socks4上线不出网主机
数字图像处理实验目录
密码技术---密钥和SSL/TLS
Tiktok actual combat ~ number of likes pop-up box
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
Eight honors and eight disgraces of the programmer version~
Simple square wave generating circuit [51 single chip microcomputer and 8253a]
Go basic constant definition and use
在SOUI里使用真窗口时使用SOUI的滚动条
Configuration clic droit pour choisir d'ouvrir le fichier avec vs Code
2016. maximum difference between incremental elements
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
C#中Linq用法汇集
Loss function~
Alibaba cloud award winning experience: how to use polardb-x
“一个优秀程序员可抵五个普通程序员!”
Quantitative analysis of PSNR, SSIM and RMSE
Sword finger offer II 099 Sum of minimum paths - double hundred code
力扣刷题(2022-6-28)