简体   繁体   中英

Gson overflows with StackOverflow exception on “$Types.resolve”

I have an ArrayList and an ArrayList and the gson works perfectly fine when retrieving the custom object ArrayList but causes a stackOverflow exception when retrieving the LinearLayout ArrayList.

Code:

public Semester(JSONObject json) throws JSONException {

coursesAsString = json.getString(JSON_COURSES);
Type coursesType = new TypeToken<ArrayList<Course>>(){}.
courses = gson.fromJson(coursesAsString, // <--- THIS PART WORKS FINE

courseLayoutArrayAsString = json.getString(JSON_COURSELAYOUTARRAY);
Type courseLayoutArrayType = new TypeToken<ArrayList<LinearLayout>>(){}.getType();
courseLayoutArray = gson.fromJson(courseLayoutArrayAsString, courseLayoutArrayType); // <---- this line
//is where the problem lies. For some reason 
//"gson.fromJson(courseLayoutArrayAsString, courseLayoutArrayType)" causes a stackOverflow exception.

}

Here is the exception:

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

and it keeps going on like this for a long time.

You are trying to serialize LinearLayouts? That's very bad practice.

You should serialize the data you need in those LinearLayouts and recreate the LinearLayout when the data is deserialized.

In data structure serializers there are two major causes of stack overflows. 1) simple depth recursion limits (too many objects deep) or 2) a infinite self-referencing loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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