类型不匹配 Kotlin 和 getStringArrayListExtra

type mismatch Kotlin and getStringArrayListExtra

我尝试在 Kotlin 中构建语音识别。 我的大部分代码都是从互联网上剪切下来的。然而我失败了。 我有一个简单的 android.xml,带有 1 个文本视图、1 个编辑视图和 1 个带有 "onclick" = mybuttondo 的按钮。

我的 Kotlin 代码:

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

我把问题写进了代码 关于编译

speechresult2 = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) 

不适用于类型不匹配...... 我尝试了几个数组列表定义(在代码中用 // 注释) none 作品

当我尝试

var speechresult2 = data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS) 

没有预定义的数组列表,编译器可以工作。但是后来我遇到了问题 例如

var spokenText = speechresult2[0] 

不起作用。相同的编译器消息。

我对 array-list 的定义有什么问题?

data?.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS)returnsArrayList?即可以为空的 ArrayList。

您可以创建一个可为 null 的 ArrayList 变量,或者如果 returns 为 null,则用一个新的 ArrayList 初始化它。

例子

// 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)