当前位置:网站首页>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
边栏推荐
猜你喜欢
ADC of stm32
情感对话识别与生成简述
Alibaba cloud award winning experience: how to use polardb-x
实现BottomNavigationView和Navigation联动
Xshell configuration xforward forwarding Firefox browser
“一个优秀程序员可抵五个普通程序员!”
Win11自动关机设置在哪?Win11设置自动关机的两种方法
Minimum spanning tree
The concepts of terminal voltage, phase voltage and line voltage in FOC vector control and BLDC control are still unclear
深度剖析数据在内存中的存储----C语言篇
随机推荐
ping域名报错unknown host,nslookup/systemd-resolve可以正常解析,ping公网地址通怎么解决?
[npuctf2020]ezlogin XPath injection
Strictly abide by the construction period and ensure the quality, this AI data annotation company has done it!
Typical case of data annotation: how does jinglianwen technology help enterprises build data solutions
設置單擊右鍵可以選擇用VS Code打開文件
Jinglianwen technology's low price strategy helps AI enterprises reduce model training costs
Boost库链接错误解决方案
情感对话识别与生成简述
Xshell configuration xforward forwarding Firefox browser
Go project operation method
提交代码流程
Distributed monitoring system ZABBIX
What can I do after buying a domain name?
golang中new与make的区别
Makefile configuration of Hisilicon calling interface
China Academy of information technology, Tsinghua University, Tencent security, cloud native security, industry university research and use strong alliance!
用matlab调用vs2015来编译vs工程
Go basic anonymous variable
CDN acceleration requires the domain name to be filed first
Explain promise usage in detail