我想将列表保存到Android SharedPreferences,但是日志说错了。 我的代码在这里:
将列表保存到SharedPreferences.I想将bean保存为对象。
public static void putBookBean(Context ctx, List<BookBean> bookList) {
if (sp == null) {
sp = ctx.getSharedPreferences("config", MODE_PRIVATE);
}
SharedPreferences.Editor editor = sp.edit();
Gson gson = new Gson();
String json = gson.toJson(bookList);
editor.putString(ConstantValue.BOOK_BEAN, json);
editor.commit();
}
加载清单:
public static ArrayList<BookBean> getBookBean(Context ctx) {
if (sp == null) {
sp = ctx.getSharedPreferences("config", MODE_PRIVATE);
}
Gson gson = new Gson();
String json = sp.getString(ConstantValue.BOOK_BEAN, null);
Type type = new TypeToken<ArrayList<BookBean>>() {
}.getType();
ArrayList<BookBean> arrayList = gson.fromJson(json, type);
return arrayList;
}
这是我的豆子:
public class BookBean {
public String bookName;
public int bookBgSrc;
public BookBean(String bookName,int bookBgSrc){
this.bookName = bookName;
this.bookBgSrc =bookBgSrc;
}
public String getBookName(){
return bookName;
}
public int getBookBgSrc(){
return bookBgSrc;
}
}
并使用:
Int mDatas[] = {R.drawable.shape_book_gold, R.drawable.shape_book_add};
String mInfos[] = {"我的账本", "添加账本"};
List<BookBean> mBookList;
mBookList = new ArrayList<BookBean>();
for (int i = 0; i < mDatas.length; i++) {
mBookList.add(new BookBean(mInfos[i], mDatas[i]));
}
if (SpUtils.getBookBean(getApplicationContext()) == null) {
SpUtils.putBookBean(getApplicationContext(), mBookList);
}
mRvAdapter = new BookSelectRvAdapter(getApplicationContext(),
SpUtils.getBookBean(getApplicationContext()));
mBookList.add(mBookList.size() - 2,
new BookBean("新增账本", R.drawable.shape_book_gold+""));
SpUtils.putBookBean(getApplicationContext(), mBookList);
日志在这里:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.chase.cn.money_of_my/activity.BookSelectActivity}: com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@52899014 failed to deserialized json object [{"bookName":"我的账本","bookBgSrc":2130837618},{"bookName":"添加账本","bookBgSrc":2130837617}] given the type java.util.ArrayList<beans.BookBean>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@52899014 failed to deserialized json object [{"bookName":"我的账本","bookBgSrc":2130837618},{"bookName":"添加账本","bookBgSrc":2130837617}] given the type java.util.ArrayList<beans.BookBean>
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at com.google.gson.Gson.fromJson(Gson.java:434)
at utils.SpUtils.getBookBean(SpUtils.java:139)
at activity.BookSelectActivity.initData(BookSelectActivity.java:61)
at activity.BookSelectActivity.onCreate(BookSelectActivity.java:43)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
感谢您检查我的代码。
答案 0 :(得分:1)
您的BookBean
需要这样写。
public class BookBean {
public String bookName;
public long bookBgSrc;
public BookBean(){
}
public BookBean(String bookName,long bookBgSrc){
this.bookName = bookName;
this.bookBgSrc =bookBgSrc;
}
public String getBookName(){
return bookName;
}
public long getBookBgSrc(){
return bookBgSrc;
}
public void setBookBgSrc(long bookBgSrc) {
this.bookBgSrc = bookBgSrc;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
}