0
我有一個奇怪的問題。 我試圖從一個活動傳遞2個可分類的類到另一個。 我以完全相同的方式定義它們,但它們都是空的。當傳遞2個相同的可分類時,1爲空
的parcable類:
class Friends implements Parcelable {
private ArrayList<Integer> ids = new ArrayList<Integer>();
private ArrayList<String> names = new ArrayList<String>();
private ArrayList<Bitmap> images = new ArrayList<Bitmap>();
public void addId(Integer id)
{
ids.add(id);
}
public void addName(String name){
names.add(name);
}
public void addImage(Bitmap img){
images.add(img);
}
public ArrayList<Integer> getIds() {
return ids;
}
public ArrayList<String> getNames() {
return names;
}
public ArrayList<Bitmap> getImages() {
return images;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeList(ids);
dest.writeList(names);
dest.writeList(images);
}
public static final Parcelable.Creator<Friends> CREATOR = new Parcelable.Creator<Friends>() {
public Friends createFromParcel(Parcel in) {
return new Friends(in);
}
public Friends[] newArray(int size) {
return new Friends[size];
}
};
public Friends(Parcel in){
in.readList(ids, null);
in.readList(names, null);
in.readList(images, null);
}
public Friends(Integer id, String name, Bitmap img) {
ids.add(id);
names.add(name);
images.add(img);
}
public Friends(){
}
發送部分:
for(Integer position : selectedIds)
{
String name = a.getItem(position).getFriendName();
int id = a.getItem(position).getFriendId();
Bitmap img = a.getItem(position).getFriendImage();
Log.e("ID",String.valueOf(id));
selectedFriends.addId(new Integer(id));
selectedFriends.addName(name);
selectedFriends.addImage(img);
}
for(int position=0;position<list.getCount(); position++)
{
String name = a.getItem(position).getFriendName();
int id = a.getItem(position).getFriendId();
Bitmap img = a.getItem(position).getFriendImage();
Log.e("All IDs",String.valueOf(id));
allFriends.addId(new Integer(id));
allFriends.addName(name);
allFriends.addImage(img);
}
b.putParcelable("selecet_friends", selectedFriends);
b.putParcelable("all_friends", allFriends);
data.putExtras(b);
兩個環路正在拼命地跑(我可以看到日誌)的,你沒有看到所有的變量都被正確初始化,一切都很好。
的Reciving部分: 我同時定義爲空:
private Friends selectedFriends = null;
private Friends allFriends = null;
和處理這樣的onResult:
Log.e("Result","Fuck ya");
Friends all_friends = (Friends)data.getParcelableExtra("all_friends");
Friends selected_friends = (Friends)data.getParcelableExtra("selected_friends");
allFriends = all_friends;
selectedFriends = selected_friends;
if(selectedFriends != null){
Log.e("is null","No");
}
if(allFriends != null){
Log.e("is all null","No");
}
有誰知道怎麼來的selectedFriends爲空時allFriends是不?
編輯: 只是一個想法,但也許是因爲我把2個parcables捆綁? 只是我添加了2捆?
哇,沒有看到一個。謝謝!我會接受你的答案,但請編輯它,並從putExtras文檔中添加:「向意圖添加一組擴展數據。鍵必須包含一個包前綴,例如com.android.contacts應用程序將使用「com.android.contacts.ShowAll」之類的名稱。 如果有人遇到同樣的問題,我給的名字不好。 –
完成。很高興能有所幫助。 –