【问题标题】:Context cannot be converted to RecognitionListener上下文无法转换为 RecognitionListener
【发布时间】:2017-11-29 06:02:28
【问题描述】:

我正在 Cordova 中实现语音识别插件。
为此,我收到了错误

incompatible types: Context cannot be converted to RecognitionListener

在构建科尔多瓦插件时。
此错误来自以下代码

context = this.cordova.getActivity();
SpeechRecognizer speech = SpeechRecognizer.createSpeechRecognizer(context);
speech.setRecognitionListener(context); // Getting error here

有解决办法吗?

【问题讨论】:

    标签: java android cordova speech-recognition


    【解决方案1】:

    您的 Activity 需要 implements RecognitionListener 并且您必须将您的通用 Context 对象转换为该接口。

    context =  this.cordova.getActivity();
    SpeechRecognizer speech = SpeechRecognizer.createSpeechRecognizer(context);
    speech.setRecognitionListener(( RecognitionListener) context); 
    

    或者你可以传递一个匿名类

    context =  this.cordova.getActivity();
    SpeechRecognizer speech = SpeechRecognizer.createSpeechRecognizer(context);
    speech.setRecognitionListener(new RecognitionListener() {}); 
    

    【讨论】:

    • 感谢您的解决方案
    猜你喜欢
    • 2015-11-05
    • 2018-05-25
    • 1970-01-01
    • 2018-05-19
    • 2023-02-08
    • 2018-02-10
    • 2014-04-03
    • 2011-06-19
    • 2011-02-14
    相关资源
    最近更新 更多