yrsend
yrsend

Reputation: 23

Kotlin - MainActivity extends AppCompatActivity, onRequestPermissionsResult function not found and cannot be overriden

I have a trial activity class Main Activity that extends AppCompatActivity. In the docs, AppCompatActivity inherits the method onRequestPermissionsResult, when I try to override the said method onRequestPermissionsResult, this error shows up

"Error:(95, 5) 'onRequestPermissionsResult' overrides nothing"

    class MainActivity : AppCompatActivity(),  ConnectionCallbacks, OnConnectionFailedListener{
    //I didnt include the other functions in the interfaces, suffice to say i have already added them in the original

    override fun onRequestPermissionsResult(requestCode : Int , permissions: Array<String>, grantResults: Array<Int>){
        println("SHOULD HAVE THIS FUNCTION")
    }
}

Upvotes: 2

Views: 6386

Answers (1)

holi-java
holi-java

Reputation: 30676

Kotlin Array<Int> is mapped to Java Integer[], IntArray is mapped to Java int[], they are different array types in Java. and you can see the mapped types in Kotlin as further.

The AppCompatActivity#onRequestPermissionsResult method signature is:

override fun onRequestPermissionsResult(requestCode : Int , 
                                        permissions: Array<String>,     
                                        grantResults: IntArray){
    //      it is IntArray rather than Array<Int>   ---^  
    TODO()
}

Rather than:

override fun onRequestPermissionsResult(requestCode : Int , 
                                        permissions: Array<String>, 
                                        grantResults: Array<Int>){
    TODO()
}

Note: if the api has promised that its parameters never be null, such as onRequestPermissionsResult then you can use an IntArray to make the parameter easy to use. Otherwise, you should use IntArray? instead.

IF you don't want to know all of the mapped types in Kotlin, there is another way that will make you override the super class methods quickly. First, put the cursor in the class, then press CTRL+O to select the methods to override. for example:

class MainActivity : AppCompatActivity(), 
                     ConnectionCallbacks,
                     OnConnectionFailedListener{
// put the cursor here, press `CTRL+O` to select which method you want to override
}

Upvotes: 5

Related Questions