当前位置:网站首页>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
边栏推荐
- Chow-Liu Tree
- Explain promise usage in detail
- Which common ports should the server open
- Distributed monitoring system ZABBIX
- 基于Pyqt5工具栏按钮可实现界面切换-2
- 实现BottomNavigationView和Navigation联动
- 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:
- Use of recyclerview with viewbinding
- Go project operation method
- Construction of Hisilicon 3559 universal platform: draw a frame on the captured YUV image
猜你喜欢
Temperature measurement and display of 51 single chip microcomputer [simulation]
Compose 中的 'ViewPager' 详解 | 开发者说·DTalk
【STL源码剖析】仿函数(待补充)
Mask R-CNN
Eight bit responder [51 single chip microcomputer]
高数有多难?AI 卷到数学圈,高数考试正确率 81%!
解决:exceptiole ‘xxxxx.QRTZ_LOCKS‘ doesn‘t exist以及mysql的my.cnf文件追加lower_case_table_names后启动报错
Tiktok actual combat ~ number of likes pop-up box
Successfully changed Splunk default URL root path
Prometheus deployment
随机推荐
面试过了,起薪16k
Application of containerization technology in embedded field
Doorplate making C language
golang入门:for...range修改切片中元素的值的另类方法
Introduction to the latest plan of horizon in April 2022
Go basic anonymous variable
Prometheus deployment
@BindsInstance在Dagger2中怎么使用
“一个优秀程序员可抵五个普通程序员!”
ADC of stm32
SQL进阶语法
Detailed explanation and application of merging and sorting
Makefile configuration of Hisilicon calling interface
Solution to boost library link error
Xshell configuration xforward forwarding Firefox browser
Ping domain name error unknown host, NSLOOKUP / system d-resolve can be resolved normally, how to Ping the public network address?
RuntimeError: no valid convolution algorithms available in CuDNN
[adjustment] postgraduate enrollment of Northeast Petroleum University in 2022 (including adjustment)
Alibaba cloud award winning experience: how to use polardb-x
Distributed monitoring system ZABBIX