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

后端 未结 1 1038
不思量自难忘°
不思量自难忘° 2021-01-11 18:19

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 ca

相关标签:
1条回答
  • 2021-01-11 18:41

    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. If you define your interface in Java, SAM conversion will start working, and your current code will work without any other changes.

    2. 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) {
              // ...
          }
      })
      
    3. 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)
          // ...
      }
      

    0 讨论(0)
提交回复
热议问题