微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

由于 Android 中的数据存储迁移,应用升级后应用崩溃

如何解决由于 Android 中的数据存储迁移,应用升级后应用崩溃

应用从 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)

解决方法

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

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

val context:Context = ... // your context 
val corruptionHandler = ReplaceFileCorruptionHandler<Preferences> {
    mutablePreferencesOf(
       // recover preferences here
    ) 
}
val dataStore = context.createDataStore("data_store",corruptionHandler)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。