0

I am trying to make a delay before executing some code on my game. I have code that runs perfectly well without a co routine, but as soon as I put:

GlobalScope.launch {
//code
}

or

GlobalScope.launch {
delay(3000L)
//code
}

The app completely crashes. The wierd thing is that sometimes it works and sometimes it doesnt. Why is this the case?

This is the stack trace:

2020-03-14 10:29:18.965 13369-13421/com.example.notagame E/AndroidRuntime: FATAL EXCEPTION: DefaultDispatcher-worker-1
    Process: com.example.notagame, PID: 13369
    android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8191)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1420)
        at android.view.View.requestLayout(View.java:24454)
        at android.view.View.requestLayout(View.java:24454)
        at android.view.View.requestLayout(View.java:24454)
        at android.view.View.requestLayout(View.java:24454)
        at android.view.View.requestLayout(View.java:24454)
        at android.view.View.requestLayout(View.java:24454)
        at androidx.constraintlayout.widget.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)
        at android.view.View.requestLayout(View.java:24454)
        at android.widget.TableLayout.requestLayout(TableLayout.java:228)
        at android.view.View.requestLayout(View.java:24454)
        at android.view.View.requestLayout(View.java:24454)
        at android.view.View.setForeground(View.java:23052)
        at com.example.notagame.Excel$onCreate$1$1.invokeSuspend(Excel.kt:140)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:241)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
        at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
        at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:740)
Mingdi
  • 185
  • 1
  • 2
  • 7
  • Use Logcat to examine the stack trace associated with your crash. If you do not understand what the stack trace means, edit your question and post it. – CommonsWare Mar 13 '20 at 20:50
  • CommonsWare edited – Mingdi Mar 13 '20 at 21:05
  • 1
    That is not a [stack trace](https://stackoverflow.com/q/3988788/115145), and it does not appear to be coming from your app. – CommonsWare Mar 13 '20 at 21:07
  • Share the rest of the code and stack trace – mad_lad Mar 14 '20 at 05:29
  • Share the rest of the code and stack trace – mad_lad Mar 14 '20 at 05:29
  • @CommonsWare Ok edited again – Mingdi Mar 14 '20 at 14:32
  • put your code in `runOnUiThread`. – John Joe Mar 14 '20 at 14:35
  • Your `//code` is attempting to modify the UI from a background thread. Generally, that is not supported. Make sure that you do the I/O or other long-running stuff on background threads but that you update your UI on the main application thread. With coroutines, this is a matter of choosing the right dispatchers for the work: `Dispatchers.Main` for the main application thread, and `Dispatchers.IO` or `Dispatchers.Default` for the long-running stuff. – CommonsWare Mar 14 '20 at 14:35

0 Answers0