我有一个带有几个字符串的对象,一个int和另一个有4个内部对象的对象。所有这些都实现了Parcelable。为了生成样板代码,我使用了Android studio中的Parcelable插件。
即使所有对象都是可以分配的,我也会收到以下错误:android.os.BadParcelableException: ClassNotFoundException when unmarshalling: AlbumTrackResponse
以下是POJO的样子:
主要对象:
public class AlbumTrackResponse implements Parcelable {
private String id;
private String albumId;
private String position;
private String title;
private int rate;
private AllServices services;
public AllServices getServices() {
return services;
}
public void setServices(AllServices services) {
this.services = services;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAlbumId() {
return albumId;
}
public void setAlbumId(String albumId) {
this.albumId = albumId;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
protected AlbumTrackResponse(Parcel in) {
id = in.readString();
albumId = in.readString();
position = in.readString();
title = in.readString();
rate = in.readInt();
services = (AllServices) in.readValue(AllServices.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(albumId);
dest.writeString(position);
dest.writeString(title);
dest.writeInt(rate);
dest.writeValue(services);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<AlbumTrackResponse> CREATOR = new Parcelable.Creator<AlbumTrackResponse>() {
@Override
public AlbumTrackResponse createFromParcel(Parcel in) {
return new AlbumTrackResponse(in);
}
@Override
public AlbumTrackResponse[] newArray(int size) {
return new AlbumTrackResponse[size];
}
};
}
具有4个对象的内部对象
public class AllServices implements Parcelable {
private GigRevService gigrev;
private AppleService apple;
private GoogleService google;
private SpotifyService spotify;
protected AllServices(Parcel in) {
gigrev = (GigRevService) in.readValue(GigRevService.class.getClassLoader());
apple = (AppleService) in.readValue(AppleService.class.getClassLoader());
google = (GoogleService) in.readValue(GoogleService.class.getClassLoader());
spotify = (SpotifyService) in.readValue(SpotifyService.class.getClassLoader());
}
@Override
public int describeContents() {
return 0;
}
public GigRevService getGigrev() {
return gigrev;
}
public void setGigrev(GigRevService gigrev) {
this.gigrev = gigrev;
}
public AppleService getApple() {
return apple;
}
public void setApple(AppleService apple) {
this.apple = apple;
}
public GoogleService getGoogle() {
return google;
}
public void setGoogle(GoogleService google) {
this.google = google;
}
public SpotifyService getSpotify() {
return spotify;
}
public void setSpotify(SpotifyService spotify) {
this.spotify = spotify;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(gigrev);
dest.writeValue(apple);
dest.writeValue(google);
dest.writeValue(spotify);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<AllServices> CREATOR = new Parcelable.Creator<AllServices>() {
@Override
public AllServices createFromParcel(Parcel in) {
return new AllServices(in);
}
@Override
public AllServices[] newArray(int size) {
return new AllServices[size];
}
};
}
此处的内部对象仅包含字符串项。
这是我如何将ArrayList传递给bundle:
bundle.putParcelableArrayList(MediaPlayerService.TRACK_LIST, trackList);
我将项目从活动传递到服务。我有什么想法可以收到此错误?
编辑:服务类的实施
public class GoogleService implements Parcelable {
private String name;
private String uri;
public String getUri() {
return uri;
}
public void setUri(String uri) {
this.uri = uri;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected GoogleService(Parcel in) {
name = in.readString();
uri = in.readString();
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(uri);
}
@SuppressWarnings("unused")
public static final Parcelable.Creator<GoogleService> CREATOR = new Parcelable.Creator<GoogleService>() {
@Override
public GoogleService createFromParcel(Parcel in) {
return new GoogleService(in);
}
@Override
public GoogleService[] newArray(int size) {
return new GoogleService[size];
}
};
}
答案 0 :(得分:0)
这看起来非常奇怪
this.services = in.readParcelable(getClass().getClassLoader());
你指出了错误的类加载器。你必须像这样使用它
this.services = (AllServices)in.readParcelable(AllServices.class.getClassLoader());
除此之外,我一直在使用此网站http://www.parcelabler.com/来快速生成我的Parceables。或者你可以像这样使用它
services = (AllServices) in.readValue(AllServices.class.getClassLoader());
答案 1 :(得分:0)
下面:
protected AllServices(Parcel in) { this.gigrev = in.readParcelable(getClass().getClassLoader());
this.apple = in.readParcelable(getClass().getClassLoader());
this.google = in.readParcelable(getClass().getClassLoader());
this.spotify = in.readParcelable(getClass().getClassLoader()); }
将其替换为
protected AllServices(Parcel in) { this.gigrev = in.readParcelable(GigRevService.class.getClassLoader());
this.apple = in.readParcelable(AppleService.class.getClassLoader());
this.google = in.readParcelable(GoogleService.class.getClassLoader());
this.spotify = in.readParcelable(SpotifyService.class.getClassLoader()); }
并替换:
this.services = in.readParcelable(getClass().getClassLoader());
使用:
this.services = in.readParcelable(AllServices.class.getClassLoader());
您还可以展示GoogleService等课程吗?每个内部对象的类也必须有一个Parcelable创建者,并且也实现Parcelable接口。