在接入融云的api时,写自定义消息,遇到了这个问题
在融云中自定义消息,要实现其Paceleable接口。
错误表现,在会话界面可以发送正常的自定义消息。
离开重返会话界面直接崩了,
定位到的错误信息看起来也很明显,没有能解析出本地的序列化化的数据,在Message的反序列方法中崩了。
错误定位:
/**
* Read and return a new Parcelable from the parcel. The given class loader
* will be used to load any enclosed Parcelables. If it is null, the default
* class loader will be used.
* @param loader A ClassLoader from which to instantiate the Parcelable
* object, or null for the default class loader.
* @return Returns the newly created Parcelable, or null if a null
* object has been written.
* @throws BadParcelableException Throws BadParcelableException if there
* was an error trying to instantiate the Parcelable.
*/
@SuppressWarnings("unchecked")
public final <T extends Parcelable> T readParcelable(ClassLoader loader) {
简单翻译:若参数不为空,将以此类加载器加载解析Parcel数据,若空,使用默认的类加载器。
返回反序列后的对象,若是NULL的对象被序列化,则反序列化也会返回空。
重点:若是空对象被写入,则解析返回的就是null.
可怕的是我跳过了注释中的那句话,
去看了这个:
*
* Exception thrown when a {@link Parcelable} is malformed or otherwise invalid.
* <p>
* This is typically encountered when a custom {@link Parcelable} object is
* passed to another process that doesn't have the same {@link Parcelable} class
* in its {@link ClassLoader}.
* 当自定义的Pacelable对象传给其他进程时,会出现当前进程加载不到当前类的情形,这是常见情形。
*/
public class BadParcelableException extends AndroidRuntimeException
好吧,有点懵。
一句话总结,序列化对象时,注意检查写入的对象注意判空处理。