2015-09-22 79 views
0

這是我的locat輸出。android.os.BadParcelableException當通過aidl傳遞對象arraylist時

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) 

什麼,我試圖做的是我有一個模型類callsed宋它實現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 + "]"; 
    } 
} 
我AIDL

我有方法

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());

+0

你有沒有試過'MediaDataManager.getInstance()。getMediasList()'? –

+0

我只需要通過aidl發送一個宋類對象的數組列表。也歡迎使用意圖以外的其他方法... –

+0

是的,我試過 –

回答

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

變化

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

使用parcelable它不接受通用數據鍵入必須使用特定的物體來包裹。

+0

我必須像這樣讀取它'data.setExtrasClassLoader(Song.class.getClassLoader()); (Collection <?extends Song>)data.getExtras()。getParcelableArrayList(「data」)' –

0

我得到了溶液通過在下面的鏈接使用意圖通過AIDL我用列表將值傳遞的提供

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

代替源代碼去宋類即,我在AIDL取代void getPlayListCallBack(in Intent data);void getPlayListCallBack(in List<Song> data);,並創建了一個Song.aidl 如下

package <package name>; 

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

現在我可以簡單地發送清單,因爲它是...希望這有助於它的人......