当前位置:网站首页>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
边栏推荐
- Generics and reflection, this is enough
- 【直播预约】数据库OBCP认证全面升级公开课
- Cryptographic technology -- key and ssl/tls
- [hardware] origin of standard resistance value
- Doorplate making C language
- [favorite poems] OK, song
- The motivation of AES Advanced Encryption Protocol
- Hisilicon VI access video process
- Prometheus deployment
- 详解Promise使用
猜你喜欢
随机推荐
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
力扣刷题(2022-6-28)
[npuctf2020]ezlogin XPath injection
密码技术---密钥和SSL/TLS
Lc173. Binary search tree iterator
设置单击右键可以选择用VS Code打开文件
第三方支付功能测试点【杭州多测师_王sir】【杭州多测师】
ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
The difference between new and make in golang
潘多拉 IOT 开发板学习(HAL 库)—— 实验4 串口通讯实验(学习笔记)
Explain promise usage in detail
Minimum spanning tree
Golang common settings - modify background
PotPlayer设置最小化的快捷键
Li Kou brush questions (2022-6-28)
Splunk audit setting
psnr,ssim,rmse三个指标的定量分析
Looking at Ctrip's toughness and vision from the Q1 financial report in 2022
【Redis笔记】压缩列表(ziplist)
情感对话识别与生成简述