I try to build a speechrecognition in Kotlin. Most of my code is cut an paste from internet. Nevertheless I fail. I have a simple android.xml with 1 textview, 1 editview and 1 button with "onclick" = mybuttondo.
my Kotlin Code:
package com.example.mytestspeech
import android.app.Activity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.speech.RecognizerIntent
import android.view.ActionMode
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*
import kotlin.collections.ArrayList
class MainActivity : AppCompatActivity() {
val SPEECHINTENTRQ = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.text = "newworld"
}
fun mybuttondo(view: View) {
// only my test for ListOf<String>
var mytext = "mystring"
var myarr = listOf<String>("one","two")
editText.setText(myarr[1])
// the speechrecognition
val speechRecognitionIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
speechRecognitionIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault().toString())
startActivityForResult(speechRecognitionIntent, SPEECHINTENTRQ)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
//var speechresult2: kotlin.collections.ArrayList<String>
var speechresult2 : kotlin.collections.ArrayList<String> = kotlin.collections.ArrayList()
//var speechresult2: ArrayList<String> = arrayListOf()
//var speechresult2: kotlin.collections.List<String> = kotlin.collections.ArrayList() // compiled successfully
//var speechresult2 = java.util.ArrayList(String)
//var spokenText: java.util.ArrayList<String> //ArrayList<String>
var speechresult: String? = String()
if (requestCode == SPEECHINTENTRQ && resultCode == Activity.RESULT_OK) {
// this works with String
speechresult = data?.getStringExtra(RecognizerIntent.EXTRA_RESULTS)
editText.setText(speechresult)
//this does not work
//no matter, which var definition from above i use
speechresult2 = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
// here I get compiler message:
//Type mismatch: inferred type is java.util.ArrayList<String!>? but kotlin.collections.ArrayList<String> /* = java.util.ArrayList<String> */ was expected
//spokenText = speechresult2[0]
//textView.text = spokenText
}
else
editText.setText("Keine Eingabe"+requestCode)
}
}
I wrote into the code the problem on compile the
speechresult2 = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
does not work with type mismatch ...... i tried several array list definitions (in code as comment with //) none of them works
When I try it with
var speechresult2 = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
without predefinied array list, compiler works. But then I have got the problem, that for example
var spokenText = speechresult2[0]
does not work. The same compiler message.
What is wrong in my definition of array-list?
Well data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)
returns ArrayList? i.e ArrayList which can be nullable.
You can create a nullable ArrayList variable or initialize it with a new ArrayList if that returns null.
Example
// to populate speech result with empty arraylist if the call returns null
val speechresult2: ArrayList<String> =
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) ?: ArrayList()
// or just take nullable array list
val speechresult2: ArrayList<String>? =
data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)