简体   繁体   中英

Android app crashes when I quit the app if Parcelised data has been used at some point, even though that data was successfully Parcelized

I have a strange crash on both the Genymotion Android simulator and my Huawei Nexus 6P . I have a screenshot that I am sending to another Anko Component successfully - obviously it is Parcelable:

@Parcelize
data class ColourBoardScreenShot(var screenshot: @RawValue BitmapDrawable) : Parcelable

However, if I take the screenshot and anytime later quit the app by pressing the circle button at the bottom of the screen the app crashes with the following stack trace:

FATAL EXCEPTION: main
Process: uk.co.davechambers.pegboard, PID: 2292
java.lang.RuntimeException: Parcel: unable to marshal value
android.graphics.drawable.BitmapDrawable@4cdb04c
at android.os.Parcel.writeValue(Parcel.java:1477)
at uk.co.davechambers.pegboard.models.ColourBoardScreenShot.writeToParcel(ColourBoardScreenShot.kt:0)
at android.os.Parcel.writeParcelable(Parcel.java:1496)
at android.os.Parcel.writeValue(Parcel.java:1402)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:154)
at android.os.Parcel.writeTypedArray(Parcel.java:1307)
at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:617)
at android.os.Parcel.writeParcelable(Parcel.java:1496)
at android.os.Parcel.writeValue(Parcel.java:1402)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:724)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
at android.os.Bundle.writeToParcel(Bundle.java:1157)
at android.os.Parcel.writeBundle(Parcel.java:764)
at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3633)
at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3773)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Can anyone tell me where I'm going wrong?

EDIT :

If I try and replace the ColourBoardScreenShot by using a plugin to generate the Parcelize code I get Unresolved reference on read and write :

data class ColourBoardScreenShot(var screenshot: @RawValue BitmapDrawable) : Parcelable {
constructor(source: Parcel) : this(
        source.read[@kotlinx.android.parcel.RawValue] BitmapDrawable ()
)

override fun describeContents() = 0

override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
    write[@kotlinx.android.parcel.RawValue] BitmapDrawable (screenshot)
}

companion object {
    @JvmField
    val CREATOR: Parcelable.Creator<ColourBoardScreenShot> = object : Parcelable.Creator<ColourBoardScreenShot> {
        override fun createFromParcel(source: Parcel): ColourBoardScreenShot = ColourBoardScreenShot(source)
        override fun newArray(size: Int): Array<ColourBoardScreenShot?> = arrayOfNulls(size)
    }
}
}

I changed my ColourBoardScreenShot to the following:

@Parcelize
data class ColourBoardScreenShot(var screenshot: ByteArray) : Parcelable

So instead of @RawValue BitmapDrawable I use ByteArray and the crash stopped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM