2

从Play商店升级应用程序后,数据存储崩溃。最近我从共享首选项迁移到数据存储。这仅发生在像素 5 中,而不发生在 android 9 中。

以下是此崩溃的日志。

Fatal Exception: d1.n.a: Unable to parse preferences proto.
at androidx.datastore.preferences.PreferencesMapCompat$Companion.readFrom(PreferencesMapCompat.java:33)
at androidx.datastore.preferences.PreferencesSerializer.readFrom(PreferencesSerializer.java:39)
at androidx.datastore.preferences.PreferencesSerializer.<clinit>(PreferencesSerializer.java:34)
at androidx.datastore.SingleProcessDataStore.readData(SingleProcessDataStore.java:239)
at androidx.datastore.SingleProcessDataStore.readDataOrHandleCorruption(SingleProcessDataStore.java:218)
at androidx.datastore.SingleProcessDataStore.readAndInitOnce(SingleProcessDataStore.java:181)
at androidx.datastore.SingleProcessDataStore$actor$1.invokeSuspend(SingleProcessDataStore.java:145)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:106)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.java:738)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.java:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.java:665)
4

1 回答 1

1

您的 proto 缓冲区文件很可能已损坏。

解决此问题的一种方法是将损坏处理程序添加到数据存储的创建中。

val context:Context = ... // your context 
val corruptionHandler = ReplaceFileCorruptionHandler<Preferences> {
    mutablePreferencesOf(
       // recover preferences here
    ) 
}
val dataStore = context.createDataStore("data_store", corruptionHandler)
于 2021-02-11T11:04:15.627 回答