0

我正在使用下面的代码开始在我的设备上收听语音命令,但是一旦我打电话,SpeechRecognizer.StartListening()我就会收到 NullPointerException。

这是完整的错误:

Java.Lang.NullPointerException:尝试在空对象引用上调用接口方法“void edu.cmu.pocketsphinx.RecognitionListener.onBeginningOfSpeech()”

正如您在下面的代码中看到的那样,我正在检查以确保我的对象引用不为空。任何人都可以在这里阐明一下吗?

public VoiceRecognizer(MainActivity mainActivity)
        {
            _mainActivity = mainActivity;
            InitializeVoiceRecognizer();
        }

    private async void InitializeVoiceRecognizer()
    {
        await SetupRecognizer();
        SwitchSearch("wakeup");
    }
    private async Task SetupRecognizer()
    {
        Java.Lang.JavaSystem.LoadLibrary("pocketsphinx_jni");
        var config = Edu.Cmu.Pocketsphinx.Decoder.DefaultConfig();
        Java.IO.File filePath = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/MyApp/voice_recognition");
        speechRecognizer = SpeechRecognizerSetup
            .DefaultSetup()
            .SetAcousticModel(new Java.IO.File(filePath, "en-us-ptm"))
            .SetDictionary(new Java.IO.File(filePath, "cmudict-en-us.dict"))
            .SetKeywordThreshold(1e-45f)
            .SetBoolean("-allphone_ci", true)
            .Recognizer;
        speechRecognizer.AddListener(this);
        speechRecognizer.AddKeywordSearch("wakeup", new Java.IO.File(filePath, "commands.lst"));            
    }
    private void SwitchSearch(string searchName)
    {
        speechRecognizer.Stop();
        if (searchName == "wakeup")
        {
            if (speechRecognizer != null)
                speechRecognizer.StartListening(searchName);
        }
    }

TIA

编辑:包括整个VoiceRecognizer班级以获得更多上下文:

using System;
using System.Threading.Tasks;
using Edu.Cmu.Pocketsphinx;


namespace ExpeditionSpotMobile.Droid.Utilities
{
    public class VoiceRecognizer : Java.Lang.Object,  IRecognitionListener
    {
        private SpeechRecognizer speechRecognizer;
        public IntPtr Handle
        {
            get; set;
        }
        MainActivity _mainActivity;

        public VoiceRecognizer()
        {

        }

        public VoiceRecognizer(MainActivity mainActivity)
        {
            _mainActivity = mainActivity;
            InitializeVoiceRecognizer();
        }

        private async Task InitializeVoiceRecognizer()
        {
            await SetupRecognizer();
            SwitchSearch("wakeup");
        }
        private async Task SetupRecognizer()
        {
            Java.Lang.JavaSystem.LoadLibrary("pocketsphinx_jni");
            var config = Edu.Cmu.Pocketsphinx.Decoder.DefaultConfig();
            Java.IO.File filePath = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory + "/MyApp/voice_recognition");
            speechRecognizer = SpeechRecognizerSetup
                .DefaultSetup()
                .SetAcousticModel(new Java.IO.File(filePath, "en-us-ptm"))
                .SetDictionary(new Java.IO.File(filePath, "cmudict-en-us.dict"))
                .SetKeywordThreshold(1e-45f)
                .SetBoolean("-allphone_ci", true)
                .Recognizer;
            speechRecognizer.AddListener(this);
            speechRecognizer.AddKeywordSearch("wakeup", new Java.IO.File(filePath, "commands.lst"));            
        }
        private void SwitchSearch(string searchName)
        {
            speechRecognizer.Stop();
            if (searchName == "wakeup")
            {
                if (speechRecognizer != null)
                    speechRecognizer.StartListening(searchName);
            }
        }

        public void Dispose()
        {
            if (speechRecognizer != null)
            {
                speechRecognizer.Cancel();
                speechRecognizer.Shutdown();
            }
        }

        public void OnBeginningOfSpeech()
        {

        }

        public void OnEndOfSpeech()
        {
            if (speechRecognizer.SearchName != "wakeup")
                SwitchSearch("wakeup");
        }

        public void Stop()
        {
            speechRecognizer.Stop();
        }

        public void OnError(Java.Lang.Exception p0)
        {
            //throw new NotImplementedException();
        }

        public void OnPartialResult(Hypothesis hypothesis)
        {
            if (hypothesis == null)
                return;
            SwitchSearch("wakeup");
        }



        public void OnResult(Hypothesis hypothesis)
        {
            if (hypothesis != null)
            {
                string text = hypothesis.Hypstr;
                // do something here
            }
        }

        public void OnTimeout()
        {
            SwitchSearch("wakeup");
        }
    }
}
4

0 回答 0