Error: onActivityResult overrides nothing

↘锁芯ラ 提交于 2019-12-20 18:07:31

问题


I am programming a speech recognition app in Kotlin for Android.

class MainActivity : AppCompatActivity() {
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val intent:Intent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
        startActivityForResult(intent, REQUEST_CODE)

    }

    override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent) {
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {/*do something*/  }
        super.onActivityResult(requestCode, resultCode, data)
    }
}

Strangly the compiler always finds the Error: 'onActivityResult' overrides nothing.

Documentation of Android states that result of startActivityForResult can be retrived with onActivityResult.

Now the question: how can one get the result of speech recognition using Kotlin?


回答1:


Replace

override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent)

With below code, to make Intent object nullable.

override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent?)

As Intent is declared nullable in parent Activity class. Here is the sample code:

protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) 


来源:https://stackoverflow.com/questions/52652998/error-onactivityresult-overrides-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!