请在回答/评论前查看编辑!
所以我有以下代码,我在asynctask的onPostExecute()方法中的活动之间传递数据,并且我的应用程序经常崩溃并出现stackoverflow错误:(注意:这不是我的完整活动)
public class Wrapper{
public Elements gradeList;
}
private class infoGetter extends AsyncTask<Void, Void, Wrapper> {
ProgressDialog progress;
public infoGetter(WelcomeLogin activity){
progress = new ProgressDialog(activity);
}
@Override
protected void onPreExecute() {
progress.setTitle("Logging in:");
progress.setMessage("Please wait...");
progress.show();
}
@Override
protected Wrapper doInBackground(Void... params) {
Wrapper w = new Wrapper();
String studentID = studentID2.getText().toString();
String username = user.getText().toString();
String password = pass.getText().toString();
String loginURL = "https://parents.mtsd.k12.nj.us/genesis/parents/j_security_check";
String userDataUrl = "https://parents.mtsd.k12.nj.us/genesis/parents?tab1=" + tab1 + "&tab2=" + tab2 + "&tab3=" + tab3 + "&studentid=" + studentID + "&action=" + action;
Response response = new GradeFetcher().getRequest(userDataUrl);
Document loggedInDocument = new GradeFetcher().postRequest(loginURL, username, password, studentID, response);
Elements grades = new GradeFetcher().gradeExtractor(loggedInDocument);
for(Element grade : grades) {
Log.e("Grades: ",grade.text());
}
w.gradeList = grades;
Log.e("MESSAGE: ","Going to onPostExecute");
return w;
}
protected void onPostExecute(Wrapper w) {
Log.e("MESSAGE: ", "Starting onPostExecute");
Gson gson = new Gson();
String serial = gson.toJson(w.gradeList,Elements.class);
Intent GradeList = new Intent(getApplicationContext(), MainList.class);
GradeList.putExtra("grades", serial);
startActivity(GradeList);
Log.e("MESSAGE: ", "Going to next activity now");
progress.dismiss();
}
}
我收到以下错误:
08-03 13:33:28.592 27871-28323/com.aurum.gradebook E/MESSAGE:﹕ Going to onPostExecute
08-03 13:33:28.604 27871-27871/com.aurum.gradebook E/MESSAGE:﹕ Starting onPostExecute
08-03 13:33:37.013 27871-27871/com.aurum.gradebook E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.aurum.gradebook, PID: 27871
java.lang.StackOverflowError: stack size 8MB
at java.io.StringWriter.write(StringWriter.java:147)
at com.google.gson.stream.JsonWriter.string(JsonWriter.java:534)
at com.google.gson.stream.JsonWriter.writeDeferredName(JsonWriter.java:402)
at com.google.gson.stream.JsonWriter.value(JsonWriter.java:417)
at com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:362)
at com.google.gson.internal.bind.TypeAdapters$13.write(TypeAdapters.java:346)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:208)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:96)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.write(CollectionTypeAdapterFactory.java:60)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:99)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:219)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRun timeTypeWrapper.java:68)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterF
我无法找到导致此情况发生的地点/原因。请帮忙!
编辑1:我现在知道我的错误在于声明:
String serial = gson.toJson(w.gradeList,Elements.class);
我认为这意味着我的&#34; w.gradeList&#34;太大了但是,这是一个简单的Jsoup Elements对象,包含大约6个废弃的等级(例如&#34; 100%),所以我不知道这是怎么回事。我知道这个错误很可能是由于循环引用,但我无法找到发生这种情况的地方。 (我也不想使用临时文件或意图以外的任何东西传递我的数据,因为他们完全有能力这样做)
答案 0 :(得分:0)
根据我的经验,您可以在Intent内的Bundle中放入1MB的数据。虽然没有记录,但android aosp中存在一个问题。 问题网址:https://code.google.com/p/android/issues/detail?id=5878
你最初的问题在于String serial = gson.toJson(w.gradeList,Elements.class);
即使你成功克服了这个错误,你也将面临GradeList.putExtra("grades", serial);
中最难的挑战,你的serial
太大而无法携带,显然它是8MB
[了解堆栈跟踪]。
如何解决此限制:
由于你显然无法超越这个限制,你现在可以做什么。将内容写入存储。我不确定您是否可以将这些数据写入SharedPreference(我相信它可以完成),但您可以在临时文件中写入,然后将文件uri发送到活动。你在那里再次阅读文件。 我希望这可以帮助你。