本文介绍了错误:onActivityResult不会覆盖任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用Kotlin为Android编写语音识别应用程序.
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)
}
}
严格来说,编译器总是会发现错误:"onActivityResult"不会覆盖任何内容.
Strangly the compiler always finds the Error: 'onActivityResult' overrides nothing.
Android文档指出,可以使用onActivityResult检索startActivityForResult的结果.
Documentation of Android states that result of startActivityForResult can be retrived with onActivityResult.
现在的问题是:如何使用Kotlin获得语音识别的结果?
Now the question: how can one get the result of speech recognition using Kotlin?
推荐答案
替换
override fun onActivityResult(requestCode:Int, resultCode:Int, data:Intent)
使用以下代码,使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)
这篇关于错误:onActivityResult不会覆盖任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!