Android Studio将Java转换为Kotlin错误无法推断此参数的类型.请明确指定 [英] Android Studio converting Java to Kotlin error Cannot infer a type for this parameter. Please specify it explicitly

查看:376
本文介绍了Android Studio将Java转换为Kotlin错误无法推断此参数的类型.请明确指定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android开发的新手(六个月前开始),并且想从Java转到Kotlin.我已经将我的项目转换为Kotlin并修复了所有问题,但是有一个问题,我不知道如何解决.

I'm new to Android development (started 6 months ago) and would like to move from Java to Kotlin. I've converted my project to Kotlin and fixed all issues but one and I can't figure out how to fix it.

我正在尝试检索JSONArray(如类JsonManager中所示),并通过方法调用在名为DataDisplayPage的第二个类中使用检索到的数据.

I'm attempting to retrieve a JSONArray (as seen in class JsonManager) and use the retrieved data in a second class called DataDisplayPage via a method call.

我收到以下控制台错误,该错误发生在第二堂课的这一行:jManager.fetch_data{ theJsonArray ->.

I am getting the following console errors, which occur at this line of the second class: jManager.fetch_data{ theJsonArray ->.

无法推断此参数的类型.请明确指定.

Cannot infer a type for this parameter. Please specify it explicitly.

类型不匹配:推断的类型为(???)->单位,但应为OnTaskCompleted

Type mismatch: inferred type is (???) -> Unit but OnTaskCompleted was expected

一流的JsonManager

First class JsonManager

interface OnTaskCompleted {
    fun onTaskCompleted(theJsonArray: JSONArray)
}

class JsonManager {
    var listener: OnTaskCompleted? = null

    init {
        Log.d("JsonManager", "Instance created")
    }

    fun fetch_data(callback : OnTaskCompleted) {
        listener = callback
         val url ="https://someURL"

        AndroidNetworking.get(url)
            .build()
            .getAsJSONArray(object : JSONArrayRequestListener {
                override fun onResponse(response: JSONArray) {
                    listener?.onTaskCompleted(response)
                }

                override fun onError(anError: ANError) {
                    Log.d("error", anError.toString())
                }
            })

}

第二类DataDisplayPage

Second class DataDisplayPage

class DataDisplayPage : AppCompatActivity()

    fun onStartUp() {

        val jManager = JsonManager()

        jManager.fetch_data{ theJsonArray  ->
            val newData = DepData()
            newData.setCellData(theJsonArray as JSONArray)
        }
    }
}

推荐答案

到目前为止,您不能使用

As of now, you can't use SAM conversion for interfaces defined in Kotlin. There are a couple things you can do to solve your problem though.

  1. 如果您使用Java定义接口,则SAM转换将开始工作,并且您的当前代码将无需任何其他更改即可工作.

  1. If you define your interface in Java, SAM conversion will start working, and your current code will work without any other changes.

如果要使用接口作为fetch_data方法的参数,并且希望使用Kotlin编写该接口,则必须传入实现该接口的object有点冗长,类似于Java的解决方案:

If you want to use an interface as the parameter of the fetch_data method, and you want it to be written in Kotlin, you'll have to pass in an object that implements it, which is a slightly verbose, Java-like solution:

jmManager.fetch_data(object: OnTaskCompleted {
    override fun onTaskCompleted(theJsonArray: JSONArray) {
        // ...
    }
})

  • 如果您想要一个不错的纯Kotlin解决方案,只需除去接口,并让fetch_data函数将函数作为参数而不是接口作为参数(同样,DataDisplayPage中的当前代码将与此一起工作):

  • If you want a nice pure Kotlin solution, just get rid of the interface, and have the fetch_data function take a function as its parameter instead of the interface (again, your current code in DataDisplayPage will work with this):

    fun fetch_data(callback: (JSONArray) -> Unit) { 
        // ...
        listener?.onTaskCompleted(response)
        callback(response)
        // ...
    }
    

    这篇关于Android Studio将Java转换为Kotlin错误无法推断此参数的类型.请明确指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆