【发布时间】:2018-06-25 23:00:02
【问题描述】:
首先,我对一般的编程比较陌生。对于学校,我正在开发一个 android 项目,我希望能够使用 SharedPreferences 保存数据,使用 Gson 将 ArrayList 转换为 Json。但是,每当我尝试运行它(模拟器)时,我都会在 logcat 中收到以下消息:
Do full code cache collection, code=125KB, data=66KB
After code cache collection, code=104KB, data=41KB
Background concurrent copying GC freed 36221(1156KB) AllocSpace objects, 3(60KB) LOS objects, 49% free, 1708KB/3MB, paused 5.863ms total 115.252ms
Do partial code cache collection, code=123KB, data=56KB
After code cache collection, code=123KB, data=56KB
Increasing code cache capacity to 512KB
这会持续一段时间,直到我的应用崩溃。以与我在这里尝试相同的方式保存其他数据时,我不会遇到此问题。
代码:
向数组中添加对象
for(int x = 0; x < board.getWidth(); x++ ) {
for( int y = 0; y < board.getHeight(); y++ ) {
Field field = (Field) MainActivity.game.getGameBoard().getObject(x, y);
fieldArray.add(field);
}
}
转换和保存
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = prefs.edit();
Gson objectGson = new Gson();
String objectJson = objectGson.toJson(fieldArray);
edits.putString("gameobjects", objectJson);
editor.apply();
这可能是什么问题?
【问题讨论】:
-
您的
Fields 中有循环引用吗?也许一些通过left, right, up, down引用定义的邻居?