通过aidl传递对象arraylist时的android.os.BadParcelableException

时间:2015-09-22 04:54:18

标签: android android-intent

这是我的locat输出。

    09-22 11:12:11.985: E/AndroidRuntime(6208): FATAL EXCEPTION: Thread-4521
    09-22 11:12:11.985: E/AndroidRuntime(6208): Process: com.acsia.mediaservice:remote, PID: 6208
    09-22 11:12:11.985: E/AndroidRuntime(6208): android.os.BadParcelableException: ClassNotFoundException when unmarshalling: com.acsia.mediaservice.model.Song
    09-22 11:12:11.985: E/AndroidRuntime(6208):     at android.os.Parcel.readException(Parcel.java:1467)
    09-22 11:12:11.985: E/AndroidRuntime(6208):     at android.os.Parcel.readException(Parcel.java:1419)
    09-22 11:12:11.985: E/AndroidRuntime(6208):     at com.acsia.hmi.IMediaHmi$Stub$Proxy.getPlayListCallBack(IMediaHmi.java:210)
    09-22 11:12:11.985: E/AndroidRuntime(6208):     at com.acsia.mediaservice.MediaNotificationHandler.broadcastPlaylist(MediaNotificationHandler.java:175)
    09-22 11:12:11.985: E/AndroidRuntime(6208):     at com.acsia.mediaservice.MediaNotificationHandler.broadcastStartUp(MediaNotificationHandler.java:161)
    09-22 11:12:11.985: E/AndroidRuntime(6208):     at com.acsia.mediaservice.MediaService$3.run(MediaService.java:253)
    09-22 11:12:11.985: E/AndroidRuntime(6208):     at java.lang.Thread.run(Thread.java:841)

我想要做的是我有一个模型类调用Song,它实现Parcelable如下:

    public class Song implements Parcelable {
    String albumID;
    String songTitle;
    String displayName;
    String duration;
    String artist;
    String songPath;
    String albumPath;

    public String getAlbumID() {
        return albumID;
    }

    public void setAlbumID(String albumID) {
        this.albumID = albumID;
    }

    public String getSongTitle() {
        return songTitle;
    }

    public void setSongTitle(String songTitle) {
        this.songTitle = songTitle;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public String getDuration() {
        return duration;
    }

    public void setDuration(String duration) {
        this.duration = duration;
    }

    public String getArtist() {
        return artist;
    }

    public void setArtist(String artist) {
        this.artist = artist;
    }

    public String getSongPath() {
        return songPath;
    }

    public void setSongPath(String songPath) {
        this.songPath = songPath;
    }

    public String getAlbumPath() {
        return albumPath;
    }

    public void setAlbumPath(String albumPath) {
        this.albumPath = albumPath;
    }

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    public Song() {
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(albumID);
        dest.writeString(songTitle);
        dest.writeString(displayName);
        dest.writeString(duration);
        dest.writeString(artist);
        dest.writeString(songPath);
        dest.writeString(albumPath);
    }

    public static final Parcelable.Creator<Song> CREATOR = new Parcelable.Creator<Song>() {
        public Song createFromParcel(Parcel in) {
            return new Song(in);
        }

        public Song[] newArray(int size) {
            return new Song[size];
        }
    }; 
    private Song(Parcel in) {
        albumID = in.readString();
        songTitle = in.readString();
        displayName = in.readString();
        duration = in.readString();
        artist = in.readString();
        songPath = in.readString();
        albumPath = in.readString();
    }

    @Override
    public String toString() {
        return "Song [albumID=" + albumID + ", songTitle=" + songTitle + ", displayName=" + displayName + ", duration="
                + duration + ", artist=" + artist + ", songPath=" + songPath + ", albumPath=" + albumPath + "]";
    }
}

在我的援助中我有方法

void getPlayListCallBack(in Intent data);

我称之为

if (mediaHmi != null && serviceConnection != null) {
                System.out.println("broadcastStartUp intent: "+from );
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra("data", (ArrayList<? extends Parcelable>) MediaDataManager.getInstance().getMediasList());
                mediaHmi.getPlayListCallBack(intent);//MediaDataManager.getInstance().getPlayList());
            }

我收到此行intent.putParcelableArrayListExtra("data", (ArrayList<? extends Parcelable>) MediaDataManager.getInstance().getMediasList());

中的错误

2 个答案:

答案 0 :(得分:0)

我通过浏览以下链接中提供的源代码获得了解决方案

https://code.google.com/p/android-aidl-ipc-rpc-example/downloads/detail?name=AIDLParcelableExample.zip

我没有使用Intent通过aidl传递值,而是使用了Song类的List,即用void getPlayListCallBack(in Intent data);替换了aidl中的void getPlayListCallBack(in List<Song> data);并创建了一个Song.aidl 如下

package <package name>;

/* Declare our Song as a class which implements the Parcelable interface */
parcelable Song;

现在我可以简单地发送列表......希望这有助于某人......

答案 1 :(得分:0)

intent.putParcelableArrayListExtra("data", (ArrayList<? extends Parcelable>) MediaDataManager.getInstance().getMediasList());
  

将其更改为

intent.putParcelableArrayListExtra("data", (ArrayList<Song>) MediaDataManager.getInstance().getMediasList());

使用parcelable它不接受通用数据类型,您必须使用特定对象作为包裹。