【问题标题】:Kotlin, Intent: None of the following functions can be called with the arguments suppliedKotlin,意图:以下函数都不能使用提供的参数调用
【发布时间】:2021-01-20 17:58:22
【问题描述】:

Intent 不适用于 RestApiService 回调。

以前它在 RegisterActivity 中工作:

fun alert(text: String) {
    val intent = Intent(this, PopUp::class.java)
    intent.putExtra("text", text)
    startActivity(intent)
}

我想将结果显示为弹出消息。 PopUp 是自定义活动视图。

当我把它放到另一个类中时,出现错误:

以下函数均不能使用参数调用

提供。 (Context!, Class!) 定义在

android.content.Intent(String!, Uri!) 定义在

android.content.Intent

import android.content.Intent
import androidx.core.content.ContextCompat.startActivity
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class RestApiService {

// MY CUSTOM INTENT! WORKED BEFORE IF TO PLACE INTO AN ACTIVITY.
fun alert(text: String) {
    val intent = Intent(this, PopUp::class.java)
    intent.putExtra("text", text)
    startActivity(intent)
}

fun addUser(userData: ModelUserRegister, onResult: (ModelUserRegister?) -> Unit){
    val retrofit = ServiceBuilder.buildService(RestApi::class.java)
    retrofit.addUser(userData).enqueue(

        object : Callback<ModelUserRegister> {

            override fun onFailure(call: Call<ModelUserRegister>, t: Throwable) {
                onResult(null)
            }

            override fun onResponse(call: Call<ModelUserRegister>, response: Response<ModelUserRegister>) {
                val addedUser = response.body()
                if (response.code() in 200..320)
                {
                    alert(response.message())
                } else {
                    alert(response.message())
                }
                onResult(addedUser)
            }
        }
    )
}
}

最新的研究给了我改变的建议:

val intent = Intent(this, PopUp::class.java)

到:

val intent = Intent(RegisterActivity.this, PopUp::class.java)

没有帮助。这个问题给那些想使用 Kotlin 的人!

【问题讨论】:

  • 你明白this是什么意思吗? Intent 构造函数的第一个参数是一个 Context。此外,您不能无缘无故拨打startActivity。它是上下文的函数。 thisstartActivity() 在您的代码位于 Activity 中时工作,因为 Activity 一个上下文。 RegisterActivity.this 用于在回调中创建意图的情况,在 Kotlin 中将是 this@RegisterActivity。但这不是您在这里遇到的问题。

标签: kotlin android-intent alert


【解决方案1】:

要创建一个新的 Intent 对象,您必须将context 作为第一个参数传递。您不能只在任何地方使用this,因为根据您使用它的位置,它可能不是context

只有当您的代码在活动中时,您才能使用thisthis@ActivityName(Java 中的ActivityName.this)。

您可以将context 参数添加到您的alert() 函数来解决此问题:

fun alert(context: Context, text: String) {
    val intent = Intent(context, PopUp::class.java)
    intent.putExtra("text", text)
    context.startActivity(intent)
}

【讨论】:

  • 谢谢,但它与我首先描述的相同。只是 this 而不是 context。这就是重点:“仅当您的代码处于活动中时”。例如,我如何发送回调文本并调用警报?在 IOS 中有一个用于活动视图控制器的类 ...
【解决方案2】:

第 1 步。 考虑到信息缺失,最简单的解决方案是使用 callbackreturn(在我的情况下为字符串?)。

class RestApiService {

    fun addUser(userData: ModelUserRegister, onResult: (ModelUserRegister?, String?)-> Unit){
        //... code
        onResult(addedUser, response.message())
    }
}

Step2. 从主Activity 调用返回值,并使用Intent 和警报View

apiService.addUser(userInfo) { modelUserRegister: ModelUserRegister?, s: String? ->
    if (s != null) {
        alert(s)
    } else {
        alert("NO TEXT AS A RESULT")
    }
}

仍在等待任何建议以在 Activity class 之外调用 Intent。

【讨论】:

    【解决方案3】:

    如果来自片段,你可以试试这个:

    val intent = Intent(requireContext, PopUp::class.java)
    

    【讨论】:

      【解决方案4】:

      您应该改用applicationContext。 Intent 或 Toast 将接受三种上下文。它们是applicationContextrequireContextthis

      您的代码应如下所示:

      val intent = Intent(application Context, Second activity::class.java)
      startActivity(intent)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-20
        • 2017-10-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多