Parcelable encountered IOException writing serializable object getactivity()

前端 未结 12 1872
故里飘歌
故里飘歌 2020-11-29 22:15

so I am getting this in logcat:

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.resources.student_list         


        
12条回答
  •  误落风尘
    2020-11-29 22:19

    The problem occurs when your custom class has for property some other class e.g. "Bitmap". What I made is to change the property field from "private Bitmap photo" to "private transient Bitmap photo". However the image is empty after I getIntent() in the receiver activity. Because of this I passed the custom class to the intent and also I've created a byte array from the image and pass it separatly to the intent:

    selectedItem is my custom object and getPlacePhoto is his method to get image. I've already set it before and now I just get it first than convert it and pass it separatly:

     Bitmap image = selectedItem.getPlacePhoto();
     image.compress(Bitmap.CompressFormat.PNG, 100, stream);
     byte[] byteArray = stream.toByteArray();
     Intent intent = new Intent(YourPresentActivity.this, 
     TheReceiverActivity.class);
     intent.putExtra("selectedItem", selectedItem);                 
     intent.putExtra("image", byteArray);
     startActivity(intent);            
    

    `

    Then in the receiver activity I get my object and the image as byte array, decode the image and set it to my object as photo property.

     Intent intent = getIntent();
     selectedItem = (ListItem) intent.getSerializableExtra("selectedItem");
     byte[] byteArray = getIntent().getByteArrayExtra("image");
     Bitmap image = BitmapFactory.decodeByteArray(byteArray, 0, 
     byteArray.length);
     selectedItem.setPhoto(image);
    

提交回复
热议问题