我有创建复杂RealmObject的问题: 它是:
class User extends RealmObject{
public String name;
public Position position;
}
class Position extends RealmObject{
public double lat;
public double lon;
}
创建代码:
User user = realm.createObject(User.class);
user.name = "xcv";
Position position = realm.createObject(Position.class);
position.lat = message.sourceCoords.lat;
position.lon = message.sourceCoords.lon;
user.position = position;
结果如下:
java.lang.StackOverflowError: stack size 1036KB
at com.google.gson.internal.$Gson$Types$WildcardTypeImpl.<init>($Gson$Types.java:539)
at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:111)
at com.google.gson.internal.$Gson$Types$WildcardTypeImpl.<init>($Gson$Types.java:553)
at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:111)
at com.google.gson.internal.$Gson$Types$WildcardTypeImpl.<init>($Gson$Types.java:546)
我做错了什么?
答案 0 :(得分:1)
你应该像这样创建你的GSON实例
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
})
.create();