Android Studio converting Java to Kotlin error Cannot infer a type for this parameter. Please specify it explicitly

Viewed 10623

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.

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.

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.

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

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())
                }
            })

}

Second class DataDisplayPage

class DataDisplayPage : AppCompatActivity()

    fun onStartUp() {

        val jManager = JsonManager()

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