2016-09-27 3 views
0

私は2つのString、1つのint、および4つの内部オブジェクトを持つ別のオブジェクトを持っています。それらのすべてがParcelableを実装しています。ボイラープレートコードを生成するために、私はAndroidスタジオからParcelableプラグインを使用しました。ParcelableオブジェクトのArrayListを渡すとエラーが発生します

すべてのオブジェクトが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つのオブジェクトを含む内側オブジェクト

0ここから

}

内側のオブジェクトは、単なる文字列の項目が含まれています。ここで

は、私がバンドルにArrayListのを渡す方法は次のとおりです。

bundle.putParcelableArrayList(MediaPlayerService.TRACK_LIST, trackList); 

私はサービスにActivityからアイテムを渡しています。なぜ私はこのエラーが発生しているすべてのアイデア?

EDIT:サービスクラスの実装

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

これは、あなたが間違ったクラスローダに指摘されている

this.services = in.readParcelable(getClass().getClassLoader());

をめちゃめちゃ奇妙に見えます。あなたはそれに加えて、この

this.services = (AllServices)in.readParcelable(AllServices.class.getClassLoader());

ようにそれを使用する必要があり、私はいつもすぐに私のParceablesを生成するには、このサイトhttp://www.parcelabler.com/を使用しています。それとも、ここでは、この

services = (AllServices) in.readValue(AllServices.class.getClassLoader()); 
+0

私があなたに与えた回線では動作しません。おそらくあなたはそれを試してみることができます。 – funkycookie

+0

@ PopAlex-Cristianまた、すべてのサブクラスでそれを修正する必要があります。それ以外に私はもう一つのアプローチを掲示しました。 –

+0

彼は彼が使用する各クラスのすべてのオブジェクトメンバーと同じことをしなければなりません。 – mmcoder10

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インターフェースも実装しなければなりません。

+0

すべてのクラスローダーをそれに応じて変更しましたが、同じエラーが発生します。 – funkycookie

+1

GoogleService AppleServiceなどでParcelableインターフェースも実装しましたか? – mmcoder10

+0

はい、上記のウェブサイトを使ってすべてのオブジェクトを再作成します – funkycookie

関連する問題