https://kotlinlang.org logo
#android
Title
# android
s

Scott

11/29/2017, 2:28 PM
A little help please? I'm having trouble with an inner class being able to use a function that is declared in the outer class. Not sure why this is happening because the tutorial I'm doing has the same thing but no error. Here is a simplified version of the code with the failing part pointed out by the comment:
Copy code
class DAO(){

    fun fetchPicture(){
        // Code...
    }

    inner class InnerClass : AsyncTask<String, Int, Bitmap>() {

        override fun doInBackground(vararg params: String?): Bitmap {
            var network = NetworkingDAO
       // Following line fails with "Unresolved Refereence: fetchPicture"
            network.fetchPicture(picture[0])
        }
    }
}
v

vince95

11/29/2017, 2:36 PM
fetchPicture() is not a function of your network object
s

Scott

11/29/2017, 2:38 PM
You mean the DAO? Sorry, I should have labeled the outer class as being the DAO because it is. Editing that...
v

vince95

11/29/2017, 2:44 PM
so, your fetchPicture belong to your DAO class or your NetworkingDAO class ?
you're defining it in your DAO class, and try to use it with a NetworkingDAO object
s

Scott

11/29/2017, 2:51 PM
yea, sorry I'm really new to this. All of this code is in NetworkingDAO.kt . Here is the actual code, with the irrelevant parts left out:
Copy code
class NetworkingDAO {
// Irrelevant bits

fun populatePicture(pictureName: String): Bitmap? {
        // Declare the return type
        var bitmap: Bitmap? = null

        // Compose a picture URI
        val pictureURI = URI_DOMAIN + "/photos/mini/$pictureName"
        val URL = URL(pictureURI)
        val inputStream = URL.openConnection().getInputStream()

        if (inputStream != null) {
            bitmap = BitmapFactory.decodeStream(inputStream)
        }

        return bitmap
    }

    inner class GetPhotoTask : AsyncTask<String, Int, Bitmap>() {

        override fun doInBackground(vararg params: String?): Bitmap {
            var network = NetworkingDAO
            network.populatePicture(picture[0])
        }
    }
}
what fails is that last line: network.populatePicture(picture[0])
v

vince95

11/29/2017, 2:57 PM
ok I think i found it
s

Scott

11/29/2017, 2:57 PM
sorry, I had the function duplicated because I was checking somethning... fixed that so that the function is no longer duplicated
v

vince95

11/29/2017, 2:57 PM
what is picture[0] ? I think you meant param[0], to get the first agrgument of your asyncTask
s

Scott

11/29/2017, 2:58 PM
trying that...
"network.populatePicture(params[0])" solves the problem with picture[0] but the call to network.populatePicture(... still fails
v

vince95

11/29/2017, 3:01 PM
what is the error message now
s

Scott

11/29/2017, 3:03 PM
I guess I should have mentioned that the function declaration at the top says that populatePicture is never used... and the error in the call at the bottom is "Unresolved Reference : populatePicture" ... I think it's a case of scope but afaik the call in the inner class should be able to see and use the declared function in the outer class.
But apparently it can't
I actually didn't have the whole inner class in that code but it still doesn't change the error of the Unresolved Reference. Here is the complete inner class:
Copy code
inner class GetPhotoTask : AsyncTask<String, Int, Bitmap>() {

        override fun doInBackground(vararg params: String?): Bitmap {
            var network = NetworkingDAO
            var bitmap = network.populatePicture(params[0])
            
            return bitmap

        }
    }
Huh... even using super@NetworkingDAO fails
Copy code
var bitmap = with(network){
                super@NetworkingDAO.populatePicture(params[0])
            }
Got it... Error Code ID10T: Bug was found in the chair to keyboard interface.
The function call was supposed to be in the main activity file, the picture vs. params was exactly what you said AND I had an issue where I was trying to put a nullable type into a not nullable variable. Thank you for your help!
👍 1
Actually, for anyone reading this later, it was the entire inner class that was supposed to be in the main activity file.