我正在使用google sheet api(https://developers.google.com/google-apps/spreadsheets/),我正在尝试传递我创建的只包含List的类,并实现Serializable。
这里是SerializableSpreadsheetList类:
public class SerializableSpreadsheetList implements Serializable {
private List<SpreadsheetEntry> spreadsheets;
public SerializableSpreadsheetList(List<SpreadsheetEntry> list){
this.spreadsheets=list;
}
public List<SpreadsheetEntry> getSpreadsheets() {
return spreadsheets;
}
}
以及启动活动的代码:
mUpdatePracticeIntent.putExtra("isSpreadsheetsSet", true);
SerializableSpreadsheetList SerializableList=new SerializableSpreadsheetList(mSpreadsheets);
mUpdatePracticeIntent.putExtra("Spreadsheets", SerializableList);
startActivity(mUpdatePracticeIntent);
当我尝试开始活动时,应用程序崩溃了。
这是Logcat:
02-18 18:03:40.280: E/AndroidRuntime(17696): FATAL EXCEPTION: main
02-18 18:03:40.280: E/AndroidRuntime(17696): java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.keeptrack.SerializableSpreadsheetList)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Parcel.writeSerializable(Parcel.java:1176)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Parcel.writeValue(Parcel.java:1130)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Parcel.writeMapInternal(Parcel.java:488)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Bundle.writeToParcel(Bundle.java:1552)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Parcel.writeBundle(Parcel.java:502)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.content.Intent.writeToParcel(Intent.java:5659)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1411)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1377)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.app.Activity.startActivityForResult(Activity.java:2833)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.app.Activity.startActivity(Activity.java:2959)
02-18 18:03:40.280: E/AndroidRuntime(17696): at com.example.keeptrack.HomeActivity$2.onClick(HomeActivity.java:112)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.view.View.performClick(View.java:2585)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.view.View$PerformClick.run(View.java:9299)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Handler.handleCallback(Handler.java:587)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Handler.dispatchMessage(Handler.java:92)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Looper.loop(Looper.java:130)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.app.ActivityThread.main(ActivityThread.java:3691)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 18:03:40.280: E/AndroidRuntime(17696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
02-18 18:03:40.280: E/AndroidRuntime(17696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
02-18 18:03:40.280: E/AndroidRuntime(17696): at dalvik.system.NativeStart.main(Native Method)
02-18 18:03:40.280: E/AndroidRuntime(17696): Caused by: java.io.NotSerializableException: com.google.gdata.data.spreadsheet.SpreadsheetEntry
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.util.LinkedList.writeObject(LinkedList.java:973)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.lang.reflect.Method.invokeNative(Native Method)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.lang.reflect.Method.invoke(Method.java:507)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1143)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:413)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1241)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
02-18 18:03:40.280: E/AndroidRuntime(17696): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
02-18 18:03:40.280: E/AndroidRuntime(17696): at android.os.Parcel.writeSerializable(Parcel.java:1171)
02-18 18:03:40.280: E/AndroidRuntime(17696): ... 21 more
我做错了什么?提前谢谢
答案 0 :(得分:1)
使用AsyncTask
AsyncTask<Void, Void, List<SpreadsheetEntry>> myTask = new AsyncTask<Void, Void, List<SpreadsheetEntry>>()
{
@Override
protected List<SpreadsheetEntry> doInBackground(Void... params)
{
List<SpreadsheetEntry> entries;
// Get your entries from internet here
return entries;
}
@Override
protected void onPostExecute(List<SpreadsheetEntry> myEntries)
{
SpreadsheetEntrySingleton.getInstance().setEntries(myEntries);
startActivity(mUpdatePracticeIntent);
}
};
myTask.execute();
<强> SpreadsheetEntrySingleton.java 强>
public class SpreadsheetEntrySingleton
{
private static volatile SpreadsheetEntrySingleton _spreadsheetEntrySingleton;
private List<SpreadsheetEntry> _entries;
private SpreadsheetEntrySingleton()
{
super();
}
public static SpreadsheetEntrySingleton getInstance()
{
if (SpreadsheetEntrySingleton._spreadsheetEntrySingleton == null)
{
synchronized (SpreadsheetEntrySingleton.class)
{
if (SpreadsheetEntrySingleton._spreadsheetEntrySingleton == null)
{
SpreadsheetEntrySingleton._spreadsheetEntrySingleton = new SpreadsheetEntrySingleton();
}
}
}
return SpreadsheetEntrySingleton._spreadsheetEntrySingleton;
}
public List<SpreadsheetEntry> getEntries()
{
return _entries;
}
public void setEntries(List<SpreadsheetEntry> list)
{
this._entries = list;
}
}
在mUpdatePracticeIntent
的OnCreate()方法中,使用SpreadsheetEntrySingleton.getInstance().getEntries()
来获取您的参赛作品。