我正在尝试用协同程序包装this ussd library。但是由于某些原因,当我用ussdController.send
包装suspendCoroutine
时,我得到了这个错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tabsamo.ussdtest, PID: 29211
java.lang.IllegalStateException: Cannot perform this action on a not sealed instance.
at android.view.accessibility.AccessibilityRecord.enforceSealed(AccessibilityRecord.java:699)
at android.view.accessibility.AccessibilityRecord.getSource(AccessibilityRecord.java:188)
at com.romellfudi.ussdlibrary.USSDService.getLeaves(USSDService.java:174)
at com.romellfudi.ussdlibrary.USSDService.setTextIntoField(USSDService.java:96)
at com.romellfudi.ussdlibrary.USSDService.send(USSDService.java:81)
at com.romellfudi.ussdlibrary.USSDController.sendData(USSDController.java:92)
at com.romellfudi.ussdlibrary.USSDController.send(USSDController.java:97)
at com.tabsamo.ussdtest.MainActivity$callUSSD$1.invokeSuspend(MainActivity.kt:54)
at com.tabsamo.ussdtest.MainActivity$callUSSD$1.invoke(Unknown Source:31)
at com.tabsamo.ussdtest.MainActivity$callUSSD$2$responseInvoke$1.invokeSuspend(MainActivity.kt:67)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:32)
at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:236)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6940)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
这是我的代码
fun callUSSD(context: Context) {
val ussdController = USSDController.getInstance(context)
val ussdCode = "*770#"
val map = hashMapOf(
"KEY_LOGIN" to HashSet(listOf<String>()),
"KEY_ERROR" to HashSet(listOf<String>())
)
suspend fun send(data: String) = suspendCoroutine<Unit> { cont ->
ussdController.send(data) { cont.resume(Unit) }
}
ussdController.callUSSDInvoke(ussdCode, map, object: USSDController.CallbackInvoke {
override fun responseInvoke(message: String) {
// this works
// ussdController.send("1478") {
// ussdController.send("1") {}
// }
// but this does not work
GlobalScope.launch(Dispatchers.Main) {
send("1478")
send("1")
}
}
override fun over(message: String) {
Toast.makeText(context, message, Toast.LENGTH_LONG).show()
}
});
}