onRequestPermissionsResult

onRequestPermissionsResult

我有一个扩展AppCompatActivity的试用 Activity 类Main Activity。在文档中,当我尝试覆盖上述方法onRequestPermissionsResult时,AppCompatActivity会继承方法onRequestPermissionsResult,
出现此错误


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

最佳答案

Kotlin Array<Int>映射到Java Integer[]IntArray映射到Java int[],它们是Java中不同的数组类型。然后您可以在Kotlin中进一步了解mapped types

AppCompatActivity#onRequestPermissionsResult方法签名为:

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

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

注意:如果api promise 其参数绝不能是null,例如onRequestPermissionsResult,那么您可以使用IntArray使该参数易于使用。否则,您应该改用IntArray?

如果不想在ktt_rstrong中了解所有Kotlin中的mapped types,则还有另一种方法可以使您快速覆盖父类(super class)方法。首先,将光标放在类中,然后按CTRL+O选择要覆盖的方法。例如:
class MainActivity : AppCompatActivity(),
                     ConnectionCallbacks,
                     OnConnectionFailedListener{
// put the cursor here, press `CTRL+O` to select which method you want to override
}

07-28 02:14