使用koitlin时使用Parcelable在android中崩溃

时间:2019-01-22 07:16:06

标签: android kotlin

  • 我没有日志,因为我没有收到任何错误,但是应用程序 我从第一个屏幕传递数据后崩溃了...
  • 这是传递数据的正确方法

ResultTodaysEvents.kt

class ResultTodaysEvents() : Parcelable{
    override fun writeToParcel(dest: Parcel?, flags: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun describeContents(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    @SerializedName("id")
    @Expose
    private var id: Int = 0
    @SerializedName("code")
    @Expose
    private var code: String? = null
    @SerializedName("start_date")
    @Expose
    private var startDate: String? = null
    @SerializedName("end_date")
    @Expose
    private var endDate: String? = null
    @SerializedName("direct_walkin_isallowed")
    @Expose
    private var directWalkinIsallowed: Boolean = false
    @SerializedName("pax_islimited")
    @Expose
    private var paxIslimited: Boolean = false
    @SerializedName("total_pax_limit")
    @Expose
    private var totalPaxLimit: Int = 0
    @SerializedName("event_ispaid")
    @Expose
    private var eventIspaid: Boolean = false
    @SerializedName("event_price")
    @Expose
    private var eventPrice: Any? = null
    @SerializedName("location_type")
    @Expose
    private var locationType: Int = 0
    @SerializedName("location_name")
    @Expose
    private var locationName: String? = null
    @SerializedName("address")
    @Expose
    private var address: String? = null
    @SerializedName("city")
    @Expose
    private var city: String? = null
    @SerializedName("state")
    @Expose
    private var state: String? = null
    @SerializedName("country")
    @Expose
    private var country: String? = null
    @SerializedName("zipcode")
    @Expose
    private var zipcode: String? = null
    @SerializedName("latitude")
    @Expose
    private var latitude: Double = 0.toDouble()
    @SerializedName("longitude")
    @Expose
    private var longitude: Double = 0.toDouble()
    @SerializedName("created_at")
    @Expose
    private var createdAt: String? = null
    @SerializedName("updated_at")
    @Expose
    private var updatedAt: String? = null
    @SerializedName("event_master_code")
    @Expose
    private var eventMasterCode: String? = null
    @SerializedName("event_master")
    @Expose
    private var eventMaster: EventMaster? = null
    @SerializedName("event_sessions")
    @Expose
    private var eventSessions: List<EventSession>? = null

    constructor(parcel: Parcel) : this() {
        id = parcel.readInt()
        code = parcel.readString()
        startDate = parcel.readString()
        endDate = parcel.readString()
        directWalkinIsallowed = parcel.readByte() != 0.toByte()
        paxIslimited = parcel.readByte() != 0.toByte()
        totalPaxLimit = parcel.readInt()
        eventIspaid = parcel.readByte() != 0.toByte()
        locationType = parcel.readInt()
        locationName = parcel.readString()
        address = parcel.readString()
        city = parcel.readString()
        state = parcel.readString()
        country = parcel.readString()
        zipcode = parcel.readString()
        latitude = parcel.readDouble()
        longitude = parcel.readDouble()
        createdAt = parcel.readString()
        updatedAt = parcel.readString()
        eventMasterCode = parcel.readString()
        eventMaster = parcel.readParcelable(EventMaster::class.java.classLoader)
        eventSessions = parcel.createTypedArrayList(EventSession)
    }

    fun getId(): Int {
        return id
    }

    fun setId(id: Int) {
        this.id = id
    }

    fun getCode(): String? {
        return code
    }

    fun setCode(code: String) {
        this.code = code
    }

    fun getStartDate(): String? {
        return startDate
    }

    fun setStartDate(startDate: String) {
        this.startDate = startDate
    }

    fun getEndDate(): String? {
        return endDate
    }

    fun setEndDate(endDate: String) {
        this.endDate = endDate
    }

    fun isDirectWalkinIsallowed(): Boolean {
        return directWalkinIsallowed
    }

    fun setDirectWalkinIsallowed(directWalkinIsallowed: Boolean) {
        this.directWalkinIsallowed = directWalkinIsallowed
    }

    fun isPaxIslimited(): Boolean {
        return paxIslimited
    }

    fun setPaxIslimited(paxIslimited: Boolean) {
        this.paxIslimited = paxIslimited
    }

    fun getTotalPaxLimit(): Int {
        return totalPaxLimit
    }

    fun setTotalPaxLimit(totalPaxLimit: Int) {
        this.totalPaxLimit = totalPaxLimit
    }

    fun isEventIspaid(): Boolean {
        return eventIspaid
    }

    fun setEventIspaid(eventIspaid: Boolean) {
        this.eventIspaid = eventIspaid
    }

    fun getEventPrice(): Any? {
        return eventPrice
    }

    fun setEventPrice(eventPrice: Any) {
        this.eventPrice = eventPrice
    }

    fun getLocationType(): Int {
        return locationType
    }

    fun setLocationType(locationType: Int) {
        this.locationType = locationType
    }

    fun getLocationName(): String? {
        return locationName
    }

    fun setLocationName(locationName: String) {
        this.locationName = locationName
    }

    fun getAddress(): String? {
        return address
    }

    fun setAddress(address: String) {
        this.address = address
    }

    fun getCity(): String? {
        return city
    }

    fun setCity(city: String) {
        this.city = city
    }

    fun getState(): String? {
        return state
    }

    fun setState(state: String) {
        this.state = state
    }

    fun getCountry(): String? {
        return country
    }

    fun setCountry(country: String) {
        this.country = country
    }

    fun getZipcode(): String? {
        return zipcode
    }

    fun setZipcode(zipcode: String) {
        this.zipcode = zipcode
    }

    fun getLatitude(): Double {
        return latitude
    }

    fun setLatitude(latitude: Double) {
        this.latitude = latitude
    }

    fun getLongitude(): Double {
        return longitude
    }

    fun setLongitude(longitude: Double) {
        this.longitude = longitude
    }

    fun getCreatedAt(): String? {
        return createdAt
    }

    fun setCreatedAt(createdAt: String) {
        this.createdAt = createdAt
    }

    fun getUpdatedAt(): String? {
        return updatedAt
    }

    fun setUpdatedAt(updatedAt: String) {
        this.updatedAt = updatedAt
    }

    fun getEventMasterCode(): String? {
        return eventMasterCode
    }

    fun setEventMasterCode(eventMasterCode: String) {
        this.eventMasterCode = eventMasterCode
    }

    fun getEventMaster(): EventMaster? {
        return eventMaster
    }

    fun setEventMaster(eventMaster: EventMaster) {
        this.eventMaster = eventMaster
    }

    fun getEventSessions(): List<EventSession>? {
        return eventSessions
    }

    fun setEventSessions(eventSessions: List<EventSession>) {
        this.eventSessions = eventSessions
    }

    companion object CREATOR : Parcelable.Creator<ResultTodaysEvents> {
        override fun createFromParcel(parcel: Parcel): ResultTodaysEvents {
            return ResultTodaysEvents(parcel)
        }

        override fun newArray(size: Int): Array<ResultTodaysEvents?> {
            return arrayOfNulls(size)
        }
    }

}

/** Passing the data **/
    private fun startEventScheduleForTodaysEvents(data: ResultTodaysEvents) {
        val eventsIntent = Intent(this, ActEventSchedule::class.java)
        eventsIntent.putExtra(Keys.EVENT_DATA,data)
        startActivity(eventsIntent)
    }


/** Get data from previous screen **/
    private fun getDataFromPrevScreen() {

        val extras = intent.extras
        if (extras != null) {
            data = extras.getParcelable(Keys.EVENT_DATA)
        }

    }

2 个答案:

答案 0 :(得分:2)

问题是这些方法:

override fun writeToParcel(dest: Parcel?, flags: Int) {
    TODO("not implemented")
}

override fun describeContents(): Int {
    TODO("not implemented")
}

IDE在此处生成的TODO是一个函数,该函数引发异常,告诉您您忘记了从何处抛出该方法的实现(您肯定应该日志)。

因此,此时您实际上需要实现Parcelable,或者实现conventional way, manually或利用Kotlin Android Extensions的Parcelize功能。

答案 1 :(得分:0)

在Kotlin中,TODOStandard.kt类的内联函数。

/**
 * Always throws [NotImplementedError] stating that operation is not implemented.
 */

@kotlin.internal.InlineOnly
public inline fun TODO(): Nothing = throw NotImplementedError()

编译前的代码

override fun writeToParcel(dest: Parcel?, flags: Int) {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

override fun describeContents(): Int {
    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}

编译后的代码

override fun writeToParcel(dest: Parcel?, flags: Int) {
    throw NotImplementedError()
}

override fun describeContents(): Int {
    throw NotImplementedError()
}

这就是您的应用崩溃的原因。要解决此错误,您必须删除这两个方法中的TODO,然后在这些方法中编写代码。