Caused by: java.io.NotSerializableException: android.os.Handler

3.8k Views Asked by At

I keep getting this Serializable error. Both of my classes implement it. I cant seem to find problem. I have gone through all classes and still cant find the cause. It only happens on older Android version, 2.3 and below. Seems to be working find on 4.0+?

Update

Right this problem is back for me.

This time i get

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = ....datastructures.model.PortfolioAccount)

PorfolioAccount contrain all GSON object and variables.

and it caused by this

Caused by: java.io.NotSerializableException: android.os.Handler

I have checked all my classes that need to be Serializable, but still cant find nothing. Like before its only happening on old Android OS versions.

3

There are 3 best solutions below

1
On

I believe that your object has an attribute that is not Serializable. Make sure that your class attributes are all implementing Serializable interface. If this in not the problem, I would recommend to implement Parcelable interface.

0
On

Probabily you're using an exception handling or method call that are not supported by older versions of Android.

Let me make an example:

So some of new Android 4 capabilities are not implenented in Android 2.3 like ImageView.setLayerType. To avoid runtime error simply:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
   setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

This approach should be used also with exceptions handling:

} catch (NetworkOnMainThreadException nomte) {
   // log this exception
} catch (SocketTimeoutException socketTimeoutException) {
   // log this exception
}

NetworkOnMainThreadException is not implemented in Android 2.3 so when the class is loaded (and not before!) the exception java.lang.VerifyError occurs during runtime.

0
On

I was digging through the code and managed to find one class that didnt have it implemented! Problem is that i share this data between 2 classes and Logcat was throwing error about the class that had serializable implemented and not the other way around!