代码之家  ›  专栏  ›  技术社区  ›  Devrath

在使用Koitlin时,使用Parcelable会在Android中崩溃。

  •  -2
  • Devrath  · 技术社区  · 7 年前
    • 我没有日志,因为我没有收到任何错误,但应用程序 当我从第一个屏幕传递数据时会崩溃…
    • 这是传递数据的正确方法吗

    今日结果.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 回复  |  直到 7 年前
        1
  •  2
  •   zsmb13    7 年前

    问题在于这些方法:

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

    这个 TODO 这里由IDE生成的是一个函数,它抛出一个异常,告诉您忘记在从中抛出方法的任何地方实现该方法(您肯定 应该 请参见日志)。

    所以在这一点上你需要做的是实际实现 Parcelable ,要么 conventional way, manually 或者利用 Parcelize Kotlin Android扩展的功能。

        2
  •  0
  •   Son Truong    7 年前

    在Kotlin, TODO 是的内联函数 Standard.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()
    }
    

    所以你的应用程序崩溃了。要解决此错误,必须删除 托多 在这两个方法中,将代码写在这些方法中。