传递到下一个活动中的 Parcelable 在 Android 上出现问题

3

我正在尝试通过信息类将数据从一个活动传递到另一个活动,为此我使用了可包含性。 在传递时通过bundle。

Activity1 - 传递 Bundle

Intent i = new Intent(getApplicationContext(), Activity2.class);
Bundle b=new Bundle();
b.putParcelable("FolderList", mainMP3List)); //***mainMP3List is MusicFolder object***
i.putExtras(b);
startActivityForResult(i, 100); 

Activity2 - Getting Bundle

if(b.containsKey("FolderList"))
mainMP3List = b.getParcelable("FolderList");//Error is the here (readBundle: bad magic number)

我无法从第二个活动访问它们。我收到以下错误消息: Bundle: readBundle:不正确的魔数
import java.util.ArrayList;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;

public class MusicFolder implements Parcelable{

private ArrayList<SongsNameList> songsList=new ArrayList<SongsNameList>();  
private ArrayList<MusicFolder> listOfFolders=new ArrayList<MusicFolder>();


public ArrayList<SongsNameList> getSongsList() {
    return songsList;
}

public void addSongInList(SongsNameList song) {
    this.songsList.add(song);
}

public ArrayList<MusicFolder> getListOfFolders() {
    return listOfFolders;
}
public void addFolderInList(MusicFolder folder) {
    this.listOfFolders.add(folder);
}


@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    //dest.writeString(category);
    Bundle b = new Bundle();
    b.putParcelableArrayList("songs", songsList);
    b.putParcelableArrayList("folders", listOfFolders);
    dest.writeBundle(b);

}

public static final Parcelable.Creator<MusicFolder> CREATOR = 
        new Parcelable.Creator<MusicFolder>() { 
    public MusicFolder createFromParcel(Parcel in) { 
        MusicFolder category = new MusicFolder();
        //category.listOfFolders = in.readString();
        Bundle b1 = in.readBundle(SongsNameList.class.getClassLoader());        
        category.songsList = b1.getParcelableArrayList("songs");

        Bundle b2 = in.readBundle(MusicFolder.class.getClassLoader());        
        category.listOfFolders = b2.getParcelableArrayList("folders");

        return category;
    }

    @Override
    public MusicFolder[] newArray(int size) {
        return new MusicFolder[size];
    }
};



  }

SongsNameList 也是可被打包的吗? - pawelzieba
是的,只要我单独检查那个,它也可以序列化。 - Ganapathy C
你能在logcat中发布readBundle: trace = ...吗?它应该在bad magic number之后。 - pawelzieba
2个回答

3

Haven't tested it but I think instead of:

    Bundle b1 = in.readBundle(SongsNameList.class.getClassLoader());        
    category.songsList = b1.getParcelableArrayList("songs");

    Bundle b2 = in.readBundle(MusicFolder.class.getClassLoader());        
    category.listOfFolders = b2.getParcelableArrayList("folders");

应该是这样的:
    Bundle b = in.readBundle(SongsNameList.class.getClassLoader());        
    category.songsList = b.getParcelableArrayList("songs");
    category.listOfFolders = b.getParcelableArrayList("folders");

是的,应该是这样的。 - Audrius

0

这可能是由于您在特定顺序中编写值/类型到您的Parcelable中,并以不同的顺序读取它(其中一个数据边界被违反)。

检查您的保存/加载顺序,如果您没有看到问题,请发布适用于您的Parcelable实现的代码供我们查看。


他已经添加了Parcelable代码。无论如何,问题在于他编写了一个单一的Bundle(带有两个ArrayList),但读取了两个Bundle。 - Audrius
他还没有添加Parcelable代码... 如果他的问题是setExtras()覆盖了bundle,那么在getParcelable()中代码不会触发,因为他已经检查了前一个键。 - Graeme

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接