当传递2个相同的Parcable类时,1为空

问题描述 投票:0回答:1

我有一个奇怪的问题。我试图将2个可分类的类从一个活动传递到另一个活动。我以完全相同的方式定义它们,但它们是null。

可分类的:

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

两个循环都在运行(我可以看到日志),你看不到的所有变量都被正确初始化,一切都很好。

接收部分:我将两者定义为null:

    private Friends selectedFriends = null;
private Friends allFriends = null;

并像这样处理onResult:

                Log.e("Result","yessss");
            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");
            }

有没有人知道当allFriends不是时,selectedFriends是怎么回事?

编辑:只是一个想法,但也许是因为我把2个parcables放在捆绑包上?我只是添加2个捆绑包?

android parcelable
1个回答
1
投票

在发送方法中,您在此行中输入错误:

b.putParcelable("selecet_friends", selectedFriends);

试试这个:

b.putParcelable("selected_friends", selectedFriends);

此外,您应该使用更具体的键名称。 putExtras()的文档说:

向intent添加一组扩展数据。键必须包含包前缀,例如app com.android.contacts将使用“com.android.contacts.ShowAll”这样的名称

© www.soinside.com 2019 - 2024. All rights reserved.