gpt4 book ai didi

android - 无法在 jetpack 数据存储类中写入首选项未找到异常

转载 作者:行者123 更新时间:2023-12-03 21:09:44 30 4
gpt4 key购买 nike

我正在使用 jetpack 数据存储来存储用户首选项。我要完美地检索数据,但是当我尝试在数据存储中写入数据时,它给了我一个错误。附加了错误,这是一个找不到类的异常,我不知道依赖项是否有问题,或者只是我。
这是我用来处理数据存储的类

enum class UiMode {

LIGHT,DARK
}

class DarkModeManager (context : Context){

private val dataStore = context.createDataStore("settings")

val uiModeFlow: Flow<UiMode> = dataStore.data
.catch {
if (it is IOException) {
it.printStackTrace()
emit(emptyPreferences())
} else {
throw it
}
}
.map { preference ->
val isDarkMode = preference[IS_DARK_MODE] ?: false

when (isDarkMode) {
true -> UiMode.DARK
false -> UiMode.LIGHT
}
}

suspend fun setUiMode(uiMode: UiMode) {
dataStore.edit { preferences ->
preferences[IS_DARK_MODE] = when (uiMode) {
UiMode.LIGHT -> false
UiMode.DARK -> true
}
}
}

companion object {
val IS_DARK_MODE = preferencesKey<Boolean>("dark_mode")
}
}
这是我试图将数据存储到数据存储中的功能
   override fun onOptionsItemSelected(item: MenuItem): Boolean {

lifecycleScope.launch {
when (isDarkMode) {
true -> {
DarkModeManager.setUiMode(UiMode.LIGHT)
removeDarkMode()

}
false -> {
DarkModeManager.setUiMode(UiMode.DARK)
applyDarkMode()
}
}
}

return super.onOptionsItemSelected(item)
}
这是我得到的错误
2020-11-13 16:22:53.398 21567-21567/com.infinity.movieapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.infinity.movieapp, PID: 21567
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/datastore/preferences/PreferencesProto$PreferenceMap;
at androidx.datastore.preferences.core.PreferencesSerializer.writeTo(PreferencesSerializer.kt:60)
at androidx.datastore.preferences.core.PreferencesSerializer.writeTo(PreferencesSerializer.kt:36)
at androidx.datastore.core.SingleProcessDataStore.writeData$datastore_core(SingleProcessDataStore.kt:296)
at androidx.datastore.core.SingleProcessDataStore.transformAndWrite(SingleProcessDataStore.kt:280)
at androidx.datastore.core.SingleProcessDataStore$actor$1.invokeSuspend(SingleProcessDataStore.kt:165)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665)
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.datastore.preferences.PreferencesProto$PreferenceMap" on path: DexPathList[[zip file "/data/app/com.infinity.movieapp-9Pi09cjXg2oV8vtvkci8Zg==/base.apk"],nativeLibraryDirectories=[/data/app/com.infinity.movieapp-9Pi09cjXg2oV8vtvkci8Zg==/lib/arm64, /system/lib64, /system/vendor/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at androidx.datastore.preferences.core.PreferencesSerializer.writeTo(PreferencesSerializer.kt:60) 
at androidx.datastore.preferences.core.PreferencesSerializer.writeTo(PreferencesSerializer.kt:36) 
at androidx.datastore.core.SingleProcessDataStore.writeData$datastore_core(SingleProcessDataStore.kt:296) 
at androidx.datastore.core.SingleProcessDataStore.transformAndWrite(SingleProcessDataStore.kt:280) 
at androidx.datastore.core.SingleProcessDataStore$actor$1.invokeSuspend(SingleProcessDataStore.kt:165) 
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) 
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:56) 
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) 
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:738) 
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) 
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) 

最佳答案

我面临同样的问题。问题是因为版本 1.0.0-alpha03 缺少 datastore-preferences-proto 库。
您可以查看问题 here .
我的临时解决方案是在等待修复之前使用以前的版本 1.0.0-alpha02。
更新
该问题已在 1.0.0-alpha04 中修复.

关于android - 无法在 jetpack 数据存储类中写入首选项未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64820232/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com