gpt4 book ai didi

传递到下一个 Activity 时 Parcelable 中的 Android 问题

转载 作者:太空狗 更新时间:2023-10-29 12:51:59 25 4
gpt4 key购买 nike

我正在尝试将我自己的Message Class 从一个 Activity 传递到另一个 Activity 为此,我正在使用 parcelable。在尝试通过 bundle 时

Activity1 - 传递包

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

Activity 2 - 获取 bundle

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

我无法从第二个 Activity 中访问它们我收到这样的错误

Bundle:readBundle:错误的魔数(Magic Number)

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];
}
};



}

最佳答案

还没有测试过,但我认为不是:

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

关于传递到下一个 Activity 时 Parcelable 中的 Android 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11346037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com