I am developing an app for android which downloads points of interest from a server using JSON strings. Everything was working fine but since I have started testing on 4.2.1, I have been getting the following error:
01-28 15:32:14.167: E/AndroidRuntime(31174): FATAL EXCEPTION: AsyncTask #1
01-28 15:32:14.167: E/AndroidRuntime(31174): java.lang.RuntimeException: An error occured while executing doInBackground()
01-28 15:32:14.167: E/AndroidRuntime(31174): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-28 15:32:14.167: E/AndroidRuntime(31174): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-28 15:32:14.167: E/AndroidRuntime(31174): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-28 15:32:14.167: E/AndroidRuntime(31174): at java.lang.Thread.run(Thread.java:856)
01-28 15:32:14.167: E/AndroidRuntime(31174): Caused by: java.lang.StackOverflowError
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:371)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:375)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:380)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:355)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:117)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.Gson.getAdapter(Gson.java:353)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.Gson.getAdapter(Gson.java:353)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:82)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:81)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:118)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:72)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.Gson.getAdapter(Gson.java:353)
01-28 15:32:14.167: E/AndroidRuntime(31174): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.<init>(ReflectiveTypeAdapterFactory.java:
The weird thing is, it works fine on every other version I have tried...
Here is the line I think it is crashing at:
Gson g = new Gson();
JSONResponse jsonResponse = g.fromJson(serverReply, JSONResponse.class); <---
and the class I am trying to create:
/**
* The JSONResponse from the server
* @author Tom
*
*/
public class JSONResponse {
private String status;
private PointOfInterest result;
/**
* Creates a JSONResponse object
* @param json
* @return
*/
static JSONResponse convertJSONToResponse(String json){
Gson gson = new Gson();
JSONResponse jsonResponse = gson.fromJson(json, JSONResponse.class);
return jsonResponse;
}
/**
* Get Point of Interest
*/
public PointOfInterest getPointofInterest(){
return result;
}
public String getStatus(){
return status;
}
/**
* Get PrivateSection, area we can put our own data
*/
/*
public String getPrivateSection(){
return privateSection;
}
*/
/**
* Creates a JSONResponse object
* @param json
* @return
*/
String convertJSONResponseToJSON(){
Gson gson = new Gson();
return gson.toJson(this);
}
}
Please note I am aware of the terrible design pattern I have employed here, this application is for a mobile apps class I am taking in college so take it easy on me !
Edit Turns out downgrading gson to 1.7.1 works for me.
Check out this bug report. http://code.google.com/p/google-gson/issues/detail?id=440
Comment #12:
I believe it was in API 17 that the internals of WeakReference and SoftReference changed to be self-referent, which would trigger this. Are you using Gson to serialize a WeakReference or SoftReference? If you are, you should write your own TypeAdapter for those types.
Something in your code is causing a circular reference and since it works up until 4.2.1 (API 17), it is most likely one of the Java classes. It still could be your code, but I'm highly doubtful.
来源:https://stackoverflow.com/questions/14565647/gson-2-2-2-causing-a-stackoverflow-on-4-2-1-only