Android: ClassNotFoundException when passing serializable object to Activity

前端 未结 3 1565
旧时难觅i
旧时难觅i 2020-12-03 05:45

I have a few crash report logs from android market similar to this:

Exception class: java.lang.ClassNotFoundException

Source method: PathClassLoader.findCla         


        
相关标签:
3条回答
  • 2020-12-03 06:03

    LinkedHashMap doesn't implement Serializable so this may be causing the error. Try either implementing a small test to serialize this class or try removing the LinkedHashMap and see if the error persists.

    0 讨论(0)
  • 2020-12-03 06:05

    Finally reached a conclusion on this issue: it was being caused by Proguard, or more specifically, because I didn't have a propper Proguard configuration.

    It turns out Proguard was changing my Serializable's classes names, which makes Class.forName(className) fail.

    I had to reconfigure my proguard.cfg file adding the following lines:

    -keep class * implements java.io.Serializable
    
    -keepclassmembers class * implements java.io.Serializable {
        static final long serialVersionUID;
        private static final java.io.ObjectStreamField[] serialPersistentFields;
        !static !transient <fields>;
        !private <fields>;
        !private <methods>;
        private void writeObject(java.io.ObjectOutputStream);
        private void readObject(java.io.ObjectInputStream);
        java.lang.Object writeReplace();
        java.lang.Object readResolve();
    }
    
    0 讨论(0)
  • 2020-12-03 06:23

    You should look into implementing Parcelable. These two links should get you started: http://developer.android.com/reference/android/os/Parcel.html http://developer.android.com/reference/android/os/Parcelable.html

    0 讨论(0)
提交回复
热议问题