Pocketsphinx空指针异常?

时间:2016-04-24 20:10:27

标签: java android nullpointerexception speech-recognition cmusphinx

我正在我的服务中实现语音识别,但我得到一个空指针异常:

java.lang.NullPointerException: Attempt to invoke virtual method 'void edu.cmu.pocketsphinx.SpeechRecognizer.addListener(edu.cmu.pocketsphinx.RecognitionListener)' on a null object reference

(下面发布完整错误)

以下是我服务的相关部分:

public class MyService extends Service implements RecognitionListener {
    SpeechRecognizer mSpeechRecognizer;  
    public MyService() {

    }    
    @Override
    public void onCreate() {
        super.onCreate();    
        try {
            Assets assets = new Assets(MyService.this); //HERE IS WHERE THE ERROR OCCURS
            File assetDir = assets.syncAssets();
            setupRecognizer(assetDir);    
            if (mSpeechRecognizer != null) {
                mSpeechRecognizer.cancel();
            }
            mSpeechRecognizer.startListening("KEYWORD");
            Log.v(TAG, "Started Listening");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setupRecognizer(File sphinxDir) {
        try {
            mSpeechRecognizer = defaultSetup()
                    .setAcousticModel(new File(sphinxDir, "en-us-ptm"))
                    .setDictionary(new File(sphinxDir, "cmudict-en-us.dict"))
                    .setBoolean("-allphone_ci", true)
                    .setKeywordThreshold(1e-40f)
                    .getRecognizer();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mSpeechRecognizer.addListener(this);
        mSpeechRecognizer.addKeyphraseSearch("KEYWORD", "Oh Mighty Computer");
    }

以下是完整错误

  

致命的例外:主要                                                                                               处理:com.example.ruchirb.appName,PID:1520                                                                                               java.lang.RuntimeException:无法创建服务   com.example.ruchirb.appName.MyService:   java.lang.NullPointerException:尝试调用虚方法'void   edu.cmu.pocketsphinx.SpeechRecognizer.addListener(edu.cmu.pocketsphinx.RecognitionListener)”   在null对象引用上                                                                                                   在   android.app.ActivityThread.handleCreateService(ActivityThread.java:2905)                                                                                                   在android.app.ActivityThread.access $ 1900(ActivityThread.java:157)                                                                                                   在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1439)                                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5527)                                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                                   在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:730)                                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)                                                                                                引起:java.lang.NullPointerException:尝试调用虚拟   方法'无效   edu.cmu.pocketsphinx.SpeechRecognizer.addListener(edu.cmu.pocketsphinx.RecognitionListener)”   在null对象引用上                                                                                                   在   com.example.ruchirb.appName.MyService.setupRecognizer(MyService.java:71)                                                                                                   在   com.example.ruchirb.appName.MyService.onCreate(MyService.java:40)                                                                                                   在   android.app.ActivityThread.handleCreateService(ActivityThread.java:2895)                                                                                                   在android.app.ActivityThread.access $ 1900(ActivityThread.java:157)                                                                                                   在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1439)                                                                                                   在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5527)                                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                                   在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:730)                                                                                                   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

为什么我收到此错误?我该如何解决?

编辑:

我感到困惑,因为logcat显示我的SpeechRecognizer对象为空,表示我在空对象上调用addListener(edu.cmu.pocketsphinx.RecognitionListener),但我执行初始化我的识别器我的#setUpRecognizer()方法。

0 个答案:

没有答案