IO异常编写可序列化对象?传递图像arraylist

时间:2018-08-15 07:54:53

标签: object runtime-error parcelable serializable

Selected_img_layout类:

 Intent i = new Intent(Selected_img_layout.this, ImagSlider.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("image_data", spacecrafts);
                i.putExtras(bundle);
                startActivity(i);

ImagSlider类:

 Bundle bundleobject = getIntent().getExtras();
 spacecrafts = (ArrayList<Spacecraft>) bundleobject.getSerializable("image_data");

航天器类:

public class Spacecraft implements Serializable {
        Uri uri;

        public Spacecraft() {
        }

        public Spacecraft(String name, Uri uri) {
            this.name = name;
            this.uri = uri;
        }

        public Uri getUri() {
            return uri;
        }

        public void setUri(Uri uri) {
            this.uri = uri;
        }
     }

错误

 java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.example.letsgo.mypdfconverter.Spacecraft)

2 个答案:

答案 0 :(得分:0)

  

替换此:

 public Spacecraft(String name, Uri uri) {
            this.name = name;
            this.uri = uri;
        }
  

具有:

 public Spacecraft( Uri uri) {
            this.uri = uri;
        }

答案 1 :(得分:0)

尝试以下代码: 意图i =新意图(Selected_img_layout.this,ImagSlider.class);                 i.putExtras(“ image_data”,spacecrafts);                 startActivity(i);

尝试此操作可将您的数据用于其他活动

Spacecraft spaceCraft =(Spacecraft)getIntent()。getSerializableExtra(“ image_data”)