为什么我得到这个错误java.util.ArrayList不能被强制转换为android.os.Parcelable

eyh26e7m  于 12个月前  发布在  Android
关注(0)|答案(2)|浏览(182)

我在这个代码上得到错误

listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);

字符串
这是我传递意图的方式

Intent intent = new Intent(mcontext,SingleObjectActivity.class);

intent.putParcelableArrayListExtra("Object_list", personArrayList);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(intent);


这是第二个活动

public class SingleObjectActivity extends Activity
{
    public static final String OBJECT_LIST = "Object_list";
    private ArrayList<Listitem> Objects;
    public ImageView imgview;
    private Listitem listObjects;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.singleobject);

        Bundle extras = getIntent().getExtras();

        imgview = (ImageView) findViewById(R.id.funnyimage);

        if (extras.containsKey(OBJECT_LIST)) {
            this.listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);
        } else {
            this.listObjects = null;
        }

        if (this.listObjects != null) {
            Picasso.
                    with(getApplicationContext()).
                    load(this.listObjects.getUrl())
                            //load()
                    .placeholder(R.drawable.logo)
                    .fit()
                    .noFade()
                    .into(imgview);

        }
    }
}


这是listitem

public class Listitem implements Parcelable {
    private int order;
    private String id;
    private String url;
    private String userName;
    private int likes;
    //String name;

   public Listitem(Parcel in){
        this.id = in.readString();
        this.url = in.readString();
       //   this.name = in.readString();

    }

    public Listitem(int order, String id, String url, String userName, int likes) {
        this.id = id;
        this.url = url;
       this.userName = userName;
       this.order = order;
       this.likes = likes;
    }

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

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }

    public int getLikes() {
        return likes;
    }

    public void setLikes(int likes) {
        this.likes = likes;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.id);
        // dest.writeString(this.name);
        dest.writeString(this.url);

    }

    public static final Parcelable.Creator<Listitem> CREATOR = new Parcelable.Creator<Listitem>() {
        public Listitem createFromParcel(Parcel in) {
            return new Listitem(in);
        }

        public Listitem[] newArray(int size) {
            return new Listitem[size];
        }
    };

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getOrder() {
        return order;
    }

    public void setOrder(int order) {
        this.order = order;
    }

ut6juiuv

ut6juiuv1#

你传递了完整的数组列表,但只得到一个对象
比如说,试试这样:
用于发送

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Intent i = new Intent(this,SecondActivity.class);

ArrayList<MyParcelable> testing = new ArrayList<MyParcelable>();

i.putParcelableArrayListExtra("extraextra", testing);
startActivity(i);
}

字符串
用于接收

public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<MyParcelable> testing = this.getIntent().getParcelableArrayListExtra("extraextra");
}
}


//只有一个成员属性的简单类作为示例

public class MyParcelable implements Parcelable {
private int mData;

/* everything below here is for implementing Parcelable */

// 99.9% of the time you can just ignore this
@Override
public int describeContents() {
    return 0;
}

// write your object's data to the passed-in Parcel
@Override
public void writeToParcel(Parcel out, int flags) {
    out.writeInt(mData);
}

// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() {
    public MyParcelable createFromParcel(Parcel in) {
        return new MyParcelable(in);
    }

    public MyParcelable[] newArray(int size) {
        return new MyParcelable[size];
    }
};

// example constructor that takes a Parcel and gives you an object populated with it's values
private MyParcelable(Parcel in) {
    mData = in.readInt();
}


}

qqrboqgw

qqrboqgw2#

你要把一个数组列表

intent.putParcelableArrayListExtra("Object_list", personArrayList);

字符串
但是你只想获取一个Parcelable:

this.listObjects = (Listitem) extras.getParcelable(OBJECT_LIST);


如果你要保存一个数组列表,那么你需要调用getParcelableArrayListExtra

相关问题