user13294271
user13294271

Reputation:

Inlining codes with coroutines to inside of button.setOnClickListener makes app crash

This is the summary of some codes which I am trying to implement.

button.setOnClickListener {
    showSomeAnimation()

    CoroutineScope(Dispatchers.IO).launch { // this line is UserActivity.kt:138
        someNetworkTasks()
        withContext(Dispatchers.Main) {
            updateUI()
        }
    }
}

However, once I press the button, java.lang.VerifyError occurs:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.diy.drift, PID: 25461
    java.lang.VerifyError: Verifier rejected class com.diy.drift.UserActivity$onCreate$$inlined$forEachIndexed$lambda$1$1: java.lang.Object com.diy.drift.UserActivity$onCreate$$inlined$forEachIndexed$lambda$1$1.invokeSuspend(java.lang.Object) failed to verify: java.lang.Object com.diy.drift.UserActivity$onCreate$$inlined$forEachIndexed$lambda$1$1.invokeSuspend(java.lang.Object): [0xDB] 'this' argument 'Uninitialized Reference: com.diy.drift.UserActivity$onCreate$$inlined$forEachIndexed$lambda$1$1$4 Allocation PC: 217' not instance of 'Precise Reference: com.diy.drift.UserActivity$onCreate$$inlined$forEachIndexed$lambda$1$1$3' (declaration of 'com.diy.drift.UserActivity$onCreate$$inlined$forEachIndexed$lambda$1$1' appears in /data/app/com.diy.drift-fU12pqcCpebCBPw2Tcgk-w==/base.apk!classes2.dex)
        at com.diy.drift.UserActivity$onCreate$$inlined$forEachIndexed$lambda$1.onClick(UserActivity.kt:138)
        at android.view.View.performClick(View.java:6612)
        at android.view.View.performClickInternal(View.java:6581)
        at android.view.View.access$3100(View.java:785)
        at android.view.View$PerformClick.run(View.java:25904)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:280)
        at android.app.ActivityThread.main(ActivityThread.java:6706)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

I thought that there can be some errors on my code and exception weren't printed definitely because the code is inside of lambda. So I extracted all of codes which are in button.setOnClickListener to a function to see what is the problem of my code like this:

fun foo() {
    showSomeAnimation()

    CoroutineScope(Dispatchers.IO).launch {
        someNetworkTasks()
        withContext(Dispatchers.Main) {
            updateUI()
        }
    }
}

button.setOnClickListener {
    foo()
}

But it worked really well without any exception. Why did this strange thing happen to me?

Upvotes: 4

Views: 569

Answers (1)

Suresh Maidaragi
Suresh Maidaragi

Reputation: 2308

Try using

GlobalScope.launch {
            someNetworkTasks()
        }
updateUI()

Hope this helps

Upvotes: 1

Related Questions