android - 如何在协同程序中开始新的 Activity ? Kotlin Android

标签 android kotlin

我的应用程序中有一个协程,它将在延迟一段时间后开始新的 Activity :

GlobalScope.launch() { 
   delay(1000L)
   startActivity(Intent(this, ThisActivity::class.java))
}

但是我在意图上说“使用提供的参数不能调用以下函数”时出现错误

我怎样才能解决这个问题?谢谢

最佳答案

问题是this引用了CoroutineScope:

GlobalScope.lauch(){
   delay(1000L)
   startActivity(Intent(this,ThisActivity::class.java))
}

您需要在此处指定上下文。如果您正在一个Activity中运行它(例如,MyActivity),则可以这样做
GlobalScope.lauch(Dispatchers.Main) {
   delay(1000L)
   startActivity(Intent(this@MyActivity,ThisActivity::class.java))
}

关于android - 如何在协同程序中开始新的 Activity ? Kotlin Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61002011/

相关文章:

android - 使用FFMPEG android用视频中的图像替换人脸

Kotlin "Any type that implements an interface"

android - InvalidObjectException ("Remote key and the prevKey should not be null") 创建 RemoteMediator android

android - 如何将包含数组的模型发布到 retrofit 车身发布中?

android - 具有Generic的Kotlin扩展功能不适用于Any吗?

generics - 在方法中使用通用类型的接口(interface)

android - Kotlin - 从对象调用类方法

Android - AnimatorSet、Object Animator - 弹跳动画链正在合并?

java - 如何使用 SQL-lite 数据库检索 android studio 中的单选按钮组

android - 在 Android ListView 中用箭头显示位置方向