2

2022-02-15 09:53:53.459 7750-7750/? E/ple.anotadomin: Unknown bits set in runtime_flags: 0x8000 2022-02-15 09:53:57.317 7750-7789/com.example.anotadomina E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1 Process: com.example.anotadomina, PID: 7750

Hello there, I'm getting this error while trying to call a fun inside a couritunescope or even global scope, i don't know why, i'm currently on the third day trying to solve the error with google with no luck.

https://github.com/Carlosktdev/Projects

java.lang.IllegalStateException: Method addObserver must be called on the main thread
        at androidx.lifecycle.LifecycleRegistry.enforceMainThreadIfNeeded(LifecycleRegistry.java:317)
        at androidx.lifecycle.LifecycleRegistry.addObserver(LifecycleRegistry.java:172)
        at androidx.lifecycle.SavedStateHandleController.attachToLifecycle(SavedStateHandleController.java:49)
        at androidx.lifecycle.SavedStateHandleController.create(SavedStateHandleController.java:70)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:67)
        at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:84)
        at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.create(HiltViewModelFactory.java:109)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
        at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
        at com.example.anotadomina.views.MainActivity.getMainViewModel(MainActivity.kt:18)
        at com.example.anotadomina.views.MainActivity.access$getMainViewModel(MainActivity.kt:14)
        at com.example.anotadomina.views.MainActivity$add$1.invokeSuspend(MainActivity.kt:36)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
        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)
Carlos
  • 23
  • 3
  • It would be nice if you could isolate the piece of code that's causing the problem and add the it to the question instead of (or in addition to) linking the whole project. Also, please post the stacktrace. You seem to be getting an exception but you're not showing the stacktrace – Joffrey Feb 15 '22 at 15:03
  • The thing is that I'm very new, so honestly i don't have any idea what is stacktrace or what is causing the error. – Carlos Feb 15 '22 at 15:11
  • Normally the stacktrace is a few lines starting with `at ` shown below the exception line in the logs. Here is a more detailed explanation about what it looks like and is used for: https://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors – Joffrey Feb 15 '22 at 15:12
  • oh ok thank you, i just added it to the question in text and image form, thank you. – Carlos Feb 15 '22 at 15:24
  • You can add 3 backticks on the line before and on the line after the code you paste to format it as a code block, so that it's readable and you don't need the image ;) Also, I'm sorry I tried to explain it quickly but the stacktrace also includes the first line with the exception information, sorry for not specifying this – Joffrey Feb 15 '22 at 15:25
  • oh nice, there is the first line now. – Carlos Feb 15 '22 at 15:41

1 Answers1

1

You're indirectly trying to call addObserver on a background thread, while you were supposed to do it on the main thread.

This is due to accessing your viewModel directly from the background coroutine here (via a viewModels() delegate):

    fun add(){
        CoroutineScope(Dispatchers.IO).launch {
            // mainViewModel property access via delegate
            mainViewModel.addScore(ScoreModel(0,15,0))
        }
    }

One solution is to get it from outside the launch, but you should probably just use the lifecycleScope instead of Dispatchers.IO:

    fun add(){
        lifecycleScope.launch {
            // mainViewModel property access via delegate
            mainViewModel.addScore(ScoreModel(0,15,0))
        }
    }

The above requires the lifecycle:lifecycle-runtime-ktx library, which you can add to your project by adding the dependency in your build.gradle.kts:

implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.0")
Joffrey
  • 32,348
  • 6
  • 68
  • 100