Java – Kotlin inner classes can’t access external private methods?

Kotlin inner classes can’t access external private methods?… here is a solution to the problem.

Kotlin inner classes can’t access external private methods?

Hello, I’m new to kotlin and am trying to simply call private functions from an inner anonymous class.

The call is as follows:

      object callback : Callback<ResponseBody> {
        override fun onResponse(call: Call<ResponseBody>?, response: Response<ResponseBody>?) {

Log.d("jjj", " response is " + response.toString())
            displaySuccess()

}

override fun onFailure(call: Call<ResponseBody>?, t: Throwable?) {
        }

}

private fun displaySuccess(){
        Toast.makeText(activity,"succesful", Toast.LENGTH_LONG)
    }

Both code fragments reside in the Fragment class and cannot access displaySuccess in onResponse?

Solution

By using object, you are creating a singleton that does not know how to access the outer class. You should define it as a class and instantiate it.

Related Problems and Solutions