简体   繁体   English

Android-gson.toJson在ArrayList上引发StackOverFlowError <OverlayItem>

[英]Android - gson.toJson throws StackOverFlowError on ArrayList<OverlayItem>

I'm trying to create a JSON string of my ArrayList containing OverlayItems ( OverlayItem is a type of osmdroid library that I'm using). 我正在尝试创建一个包含OverlayItems ArrayList的JSON字符串( OverlayItem是我正在使用的一种osmdroid库)。 The reason I'm doing this is because I want to keep this ArrayList of OverlayItems when the Android app is closed, the data must be kept (and the gson-solution seemed like a nice way of achieving this since you can't just add an ArrayList to SharedPreferences ). 我这样做的原因是因为我想在关闭Android应用程序时保留这个ArrayList of OverlayItems ,必须保留数据(而gson-solution似乎是实现此目的的一种好方法,因为您不能仅仅添加ArrayListSharedPreferences )。

This throws a StackOverflowException error, I think it cannot resolve the type. 这引发了StackOverflowException错误,我认为它无法解析类型。 I looked up my problem and many answers on other threads suggest that this is caused by a circular reference. 我查找了我的问题,其他线程上的许多答案都表明这是由循环引用引起的。 I don't think that this is the case however with the OverlayItems , they are built like this: 我认为不是这种情况,但是对于OverlayItems ,它们的构建方式如下:

OverlayItem(String aUid, String aTitle, String aDescription, IGeoPoint aGeoPoint)

IGeoPoint is also a type of osmdroid, it just holds a longitude and latitude int. IGeoPoint也是osmdroid的一种,它仅包含经度和纬度int。

The code for the serializing is this: 序列化的代码是这样的:

SharedPreferences.Editor ed = mPrefs.edit();
Gson gson = new Gson();
String json = gson.toJson(waypointMarkers);
ed.putString("waypoints", json);
ed.commit();

// wayPointmarkers is the ArrayList<OverlayItem>

Why am I getting these errors?: 为什么会出现这些错误?:

java.lang.StackOverflowError
        at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
        at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
        at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
        at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
         ...

Edit 1 编辑1

I've added InstanceCreators for both OverlayItem and IGeoPoint (neither of them has a default no-args constructor): 我为OverlayItem和IGeoPoint添加了InstanceCreator(它们都没有默认的无参数构造函数):

class OverlayItemInstanceCreator implements InstanceCreator<OverlayItem> {
    public OverlayItem createInstance(Type type) {
        return new OverlayItem(null,null,null,null);
    }
}

class IGeoPointItemInstanceCreator implements InstanceCreator<IGeoPoint> {
    public IGeoPoint createInstance(Type type) {
        return new IGeoPoint() {
            @Override
            public int getLatitudeE6() {
                return 0;
            }

            @Override
            public int getLongitudeE6() {
                return 0;
            }

            @Override
            public double getLatitude() {
                return 0;
            }

            @Override
            public double getLongitude() {
                return 0;
            }
        };
    }
}

And I'm registering them with this code: 我正在用以下代码注册它们:

GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(OverlayItem.class, new OverlayItemInstanceCreator());
    gsonBuilder.registerTypeAdapter(IGeoPoint.class, new IGeoPointItemInstanceCreator());
    Gson gson = gsonBuilder.create();
    String json = gson.toJson(waypointMarkers);
    ed.putString("waypoints", json);

But I'm still getting the same errors. 但是我仍然遇到相同的错误。 What am I doing wrong? 我究竟做错了什么?

Looks like you are trying to serialize classes without default constructor (eg GeoPoint implementation doesn't have one). 看起来您正在尝试在没有默认构造函数的情况下序列化类(例如, GeoPoint实现没有一个)。 Gson can't do it without additional work (actually can, but in some quirky manner and it's unsafe). Gson不能不做额外的工作(实际上可以,但是以某种古怪的方式,这是不安全的)。

Check out this thread , it can be helpful. 查看此线程 ,可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM