Problem unmarshalling parcelables

后端 未结 10 1927
旧时难觅i
旧时难觅i 2020-11-27 15:45

I\'ve got a few classes that implement Parcelable and some of these classes contain each other as properties. I\'m marshalling the classes into a Parcel to pass them between

10条回答
  •  独厮守ぢ
    2020-11-27 16:20

    I had the same problem with the following setup: some handler creates a Message and sends its over a Messenger to a remote service.

    the Message contains a Bundle where I put my Parcelable descendant:

    final Message msg = Message.obtain(null, 0);
    msg.getData().putParcelable("DOWNLOADFILEURLITEM", downloadFileURLItem);
    
    messenger.send(msg);
    

    I had the same exception when the remote service tried to unparcel. In my case, I had overseen that the remote service is indeed a separate os process. Therefore, I had to set the current classloader to be used by the unparcelling process on the service side:

    final Bundle bundle = msg.getData();
    bundle.setClassLoader(getClassLoader());
    
    DownloadFileURLItem urlItem = (DownloadFileURLItem)
    bundle.getParcelable("DOWNLOADFILEURLITEM");
    

    Bundle.setClassLoader sets the classloader which is used to load the appropriate Parcelable classes. In a remote service, you need to reset it to the current class loader.

提交回复
热议问题