How to serialize null value when using Parcelable interface

前端 未结 4 596
抹茶落季
抹茶落季 2020-12-08 09:35

regarding my code example down, what shold I do if one Locable\'s variables is null? In example, now if l.getZoom() returns null, I got NullPointerException.



        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-08 09:49

    I'm using a Parcelable class that has Integer and Boolean fields as well, and those fields can be null.

    I had trouble using the generic Parcel.writeValue method, particularly when I was trying to read it back out via Parcel.readValue. I kept getting a runtime exception that said it couldn't figure out the type of the parceled object.

    Ultimately, I was able to solve the problem by using Parcel.writeSerializable and Parcel.readSerializable with a type cast, as both Integer and Boolean implement the Serializable interface. The read and write methods handle null values for you.

提交回复
热议问题