反序列化序列化对象时抛出NotSerializableException

时间:2014-08-06 09:23:53

标签: java android serialization notserializableexception

我有一个非常奇怪的问题。我加载的一些数据我想保存为序列化对象。我以前做过这个,但由于某种原因我无法让它发挥作用。

奇怪的是,在对象的SERIALIZING中抛出了没有异常,这让我觉得问题不在于Serializable对象。只有在序列化对象中读取(加载)时才会抛出异常......

当您查看堆栈跟踪时,第一行引用的是OfflineDataHelpers类,它不可序列化。我知道,但我甚至不希望它。只有用户应该这样,所以我不明白为什么会这么说。

用户类(可序列化对象)

public class User implements Serializable{

 private String username;
 private String remember;
 private String fullname;
 private String email;
 private int id;
 private String session_id;

 public User() {
 }

 @Override
 public String toString() {
     return "User{" +
             "id='" + id + '\'' +
             ", fullname='" + fullname + '\'' +
             '}';
 }

 public String getUsername() {
     return username;
 }

 public void setUsername(String username) {
     this.username = username;
 }

 public String getRemember() {
     return remember;
 }

 public void setRemember(String remember) {
     this.remember = remember;
 }

 public String getFullname() {
     return fullname;
 }

 public void setFullname(String fullname) {
     this.fullname = fullname;
 }

 public String getEmail() {
     return email;
 }

 public void setEmail(String email) {
     this.email = email;
 }

 public int getId() {
     return id;
 }

 public void setId(int id) {
     this.id = id;
 }

 public String getSession_id() {
     return session_id;
 }

 public void setSession_id(String session_id) {
     this.session_id = session_id;
 }

 public boolean isNotNull(){
     if (this.getId() >0 && this.getUsername().length()>1){
         return true;
     }
     return false;
 }
}

OfflineDataHelpers类(执行(反)序列化的类

public class OfflineDataHelpers{
 private Context context;
 private String USERFILENAME = "User.msl";

 public OfflineDataHelpers(Context context) {
     this.context = context;
 }


 public void saveUser(User user) {
     try {
         FileOutputStream fos = new FileOutputStream(getFileDir(USERFILENAME), true);
         ObjectOutputStream os = new ObjectOutputStream(fos);
         os.writeObject(user);
         os.close();
         System.out.println(user+" has been saved");
     } catch (Exception e) {
         e.printStackTrace();
     }
 }

 public User loadUser() {
     User user = new User();
     try {
         FileInputStream fis = new FileInputStream(getFileDir(USERFILENAME));
         ObjectInputStream is = new ObjectInputStream(fis);
         user = (User) is.readObject();
         is.close();
         System.out.println(user+ " is loaded");
     } catch (Exception e) {
         e.printStackTrace();
     }
     return user;
 }

 public File getFileDir(String fileName) {
     return new File(context.getExternalFilesDir(null), fileName);
 }
}

在具有有效User对象(不为null)的活动中调用saveUser和loadUser函数。如果我删除" loadUser"电话,没有异常被抛出,它只是说:"用户....保存"

但是当我添加函数loadUser时,stacktrace中会出现以下内容:

  08-06 11:19:12.971  12097-12097/com.example.myapp W/System.err﹕ java.io.WriteAbortedException: Read an exception; java.io.NotSerializableException: com.example.myapp.others.OfflineDataHelpers
08-06 11:19:12.971  12097-12097/com.example.myapp W/System.err﹕ at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:779)
08-06 11:19:12.971  12097-12097/com.example.myapp W/System.err﹕ at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1981)
08-06 11:19:12.971  12097-12097/com.example.myapp W/System.err﹕ at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1938)
08-06 11:19:12.971  12097-12097/com.example.myapp W/System.err﹕ at com.example.myapp.others.OfflineDataHelpers.loadUser(OfflineDataHelpers.java:43)
08-06 11:19:12.971  12097-12097/com.example.myapp W/System.err﹕ at com.example.myapp.others.NetworkingController.LoginRequest(NetworkingController.java:50)
08-06 11:19:12.971  12097-12097/com.example.myapp W/System.err﹕ at com.example.myapp.fragments.InlogFragment.onActivityCreated(InlogFragment.java:77)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1508)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:958)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:446)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.os.Looper.loop(Looper.java:136)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5017)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ Caused by: java.io.NotSerializableException: com.example.myapp.others.OfflineDataHelpers
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671)
08-06 11:19:12.981  12097-12097/com.example.myapp W/System.err﹕ at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at com.example.myapp.others.OfflineDataHelpers.saveUser(OfflineDataHelpers.java:29)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at com.example.myapp.others.NetworkingController$AsyncLogin.doInBackground(NetworkingController.java:108)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at com.example.myapp.others.NetworkingController$AsyncLogin.doInBackground(NetworkingController.java:60)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-06 11:19:12.991  12097-12097/com.example.myapp W/System.err﹕ at java.lang.Thread.run(Thread.java:841)

这表明我尝试序列化OfflineDataHelpers,但我不认为我这样做。 有人有想法吗?

0 个答案:

没有答案