将分页库从 3.0.0-alpha10 更新到 3.0.0-alpha12 时出现运行时错误

getting runtime error when updating paging library from 3.0.0-alpha10 to 3.0.0-alpha12

我用 paging 3.0.0-alpha10 创建了我的 pagingSource class 并且它有效但是当我将版本更改为 3.0.0-alpha12 时, 我收到这个错误 这是运行时异常:

java.lang.AbstractMethodError: abstract method "java.lang.Object androidx.paging.PagingDataDiffer.presentNewList(androidx.paging.NullPaddedList, androidx.paging.NullPaddedList, androidx.paging.CombinedLoadStates, int, kotlin.jvm.functions.Function0, kotlin.coroutines.Continuation)"
        at androidx.paging.PagingDataDiffer$collectFrom$invokeSuspend$$inlined$collect$lambda.invokeSuspend(PagingDataDiffer.kt:136)
        at androidx.paging.PagingDataDiffer$collectFrom$invokeSuspend$$inlined$collect$lambda.invoke(Unknown Source:10)
        at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
        at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:161)
        at kotlinx.coroutines.BuildersKt.withContext(Unknown Source:1)
        at androidx.paging.PagingDataDiffer$collectFrom$invokeSuspend$$inlined$collect.emit(Collect.kt:133)
        at kotlinx.coroutines.flow.FlowKt__ChannelsKt.emitAllImpl$FlowKt__ChannelsKt(Channels.kt:61)
        at kotlinx.coroutines.flow.FlowKt__ChannelsKt$emitAllImpl.invokeSuspend(Unknown Source:11)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at kotlinx.coroutines.EventLoop.processUnconfinedEvent(EventLoop.common.kt:69)
        at kotlinx.coroutines.DispatchedTaskKt.resumeUnconfined(DispatchedTask.kt:236)
        at kotlinx.coroutines.DispatchedTaskKt.dispatch(DispatchedTask.kt:161)
        at kotlinx.coroutines.CancellableContinuationImpl.dispatchResume(CancellableContinuationImpl.kt:362)
        at kotlinx.coroutines.CancellableContinuationImpl.completeResume(CancellableContinuationImpl.kt:479)
        at kotlinx.coroutines.channels.AbstractChannel$ReceiveElement.completeResumeReceive(AbstractChannel.kt:899)
        at kotlinx.coroutines.channels.ArrayChannel.offerInternal(ArrayChannel.kt:84)
        at kotlinx.coroutines.channels.AbstractSendChannel.send(AbstractChannel.kt:135)
        at androidx.paging.PageFetcherSnapshot.doInitialLoad(PageFetcherSnapshot.kt:315)
        at androidx.paging.PageFetcherSnapshot$doInitialLoad.invokeSuspend(Unknown Source:11)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6543)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)

这是我的 PaginSource:

import androidx.paging.PagingSource
import com.example.template.api.MyApi
import com.example.template.api.responses.ChatHistoryResponse
import retrofit2.HttpException
import java.io.IOException

class ChatHistoryPagingSource
constructor(
    private val myApi: MyApi,
    private val search: String,
    private val type: String,
    private val token: String,
    private val status: String,
    ): PagingSource<Int, ChatHistoryResponse>() {

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ChatHistoryResponse> {
        val position = params.key ?: 1
        return try {
            val response = myApi.chatHistoryList(token, status, position, 20, type, search)
            LoadResult.Page(
                data = response,
                prevKey = if (position == 1) null else position -1,
                nextKey = if (response.isEmpty()) null else position +1
            )

        } catch (exception: IOException) {
            LoadResult.Error(exception)
        } catch (exception: HttpException) {
            LoadResult.Error(exception)
        }
    }

}

它在以前的版本中完全可用,但我必须迁移到新版本。

很有可能,您使用的 gradle 依赖项彼此不兼容。

在我的例子中,异常发生在我使用 androidx.room:room-paging:2.4.0-alpha05androidx.paging:paging-compose:1.0.0-alpha12 时。我通过将 paging compose 的版本更改为 1.0.0-alpha13.

来修复它