Java 在Pocketsphinx演示中切换到搜索时出现空指针异常
我想用android开发一个语音识别器。我在android设备中使用过语音识别功能。Java 在Pocketsphinx演示中切换到搜索时出现空指针异常,java,android,android-asynctask,speech-recognition,pocketsphinx-android,Java,Android,Android Asynctask,Speech Recognition,Pocketsphinx Android,我想用android开发一个语音识别器。我在android设备中使用过语音识别功能。 这是我的密码: MainActivity.java: package com.example.pocket_sphinx; import edu.cmu.pocketsphinx.Hypothesis; import edu.cmu.pocketsphinx.RecognitionListener; import android.os.Bundle; import android.app.Activity;
这是我的密码:
MainActivity.java:
package com.example.pocket_sphinx;
import edu.cmu.pocketsphinx.Hypothesis;
import edu.cmu.pocketsphinx.RecognitionListener;
import android.os.Bundle;
import android.app.Activity;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import static android.widget.Toast.makeText;
import static edu.cmu.pocketsphinx.SpeechRecognizerSetup.defaultSetup;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import edu.cmu.pocketsphinx.Assets;
import edu.cmu.pocketsphinx.SpeechRecognizer;
public class MainActivity extends Activity implements RecognitionListener {
private static final String KWS_SEARCH = "wakeup";
private static final String DICTATION_SEARCH = "digits";
private static final String KEYPHRASE = "oh mighty computer";
private SpeechRecognizer recognizer;
private HashMap<String, Integer> captions;
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
// Prepare the data for UI
captions = new HashMap<String, Integer>();
setContentView(R.layout.activity_main);
((TextView) findViewById(R.id.caption_text))
.setText("Preparing the recognizer");
// Recognizer initialization is a time-consuming and it involves IO,
// so we execute it in async task
new AsyncTask<Void, Void, Exception>() {
@Override
protected Exception doInBackground(Void... params) {
try {
Assets assets = new Assets(MainActivity.this);
File assetDir = assets.syncAssets();
setupRecognizer(assetDir);
recognizer.startListening(KWS_SEARCH);
} catch (IOException e) {
return e;
}
return null;
}
@Override
protected void onPostExecute(Exception result) {
if (result != null) {
((TextView) findViewById(R.id.caption_text))
.setText("Failed to init recognizer " + result);
} else {
switchSearch(KWS_SEARCH);
}
}
}.execute();
}
@Override
public void onPartialResult(Hypothesis hypothesis) {
String text = hypothesis.getHypstr();
Log.d("Spoken text",text);
if (text.equals(KEYPHRASE))
switchSearch(DICTATION_SEARCH);
else
((TextView) findViewById(R.id.result_text)).setText(text);
}
@Override
public void onResult(Hypothesis hypothesis) {
((TextView) findViewById(R.id.result_text)).setText("");
if (hypothesis != null) {
String text = hypothesis.getHypstr();
makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBeginningOfSpeech() {
}
@Override
public void onEndOfSpeech() {
Log.d("end","In end of speech");
if (DICTATION_SEARCH.equals(recognizer.getSearchName()))
switchSearch(KWS_SEARCH);
}
private void switchSearch(String searchName) {
recognizer.stop();
recognizer.startListening(searchName);
String caption = getResources().getString(captions.get(searchName));
((TextView) findViewById(R.id.caption_text)).setText(caption);
}
private void setupRecognizer(File assetsDir) {
File modelsDir = new File(assetsDir, "models");
recognizer = defaultSetup()
.setAcousticModel(new File(modelsDir, "hmm/en-us"))
.setDictionary(new File(modelsDir, "dict/cmu07a.dic"))
.setRawLogDir(assetsDir).setKeywordThreshold(1e-20f)
//.setFloat("-beam", 1e-30f)
.getRecognizer();
recognizer.addListener(this);
// Create keyword-activation search.
recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
// Create language model search.
File languageModel = new File(modelsDir, "lm/weather.dmp");
recognizer.addNgramSearch(DICTATION_SEARCH, languageModel);
}
}
您的代码有两个问题: 1) 您在doInBackground中开始搜索,然后在onPostExecute中再次开始相同的搜索,您可以在onPostExecute中调用switchSearch,这就足够了 2) 您不填充标题数组,但在switchSearch方法中使用它。所以您得到了空指针异常。你可以发表评论
String caption = getResources().getString(captions.get(searchName));
((TextView) findViewById(R.id.caption_text)).setText(caption);
在switchSearch中,如果您不打算使用标题,则实际错误在logcat的上面几行。您需要发布完整的日志,而不是日志的一部分。@NikolayShmyrev感谢您的关注!我修改了it@NikolayShmyrev此应用程序必须通过麦克风或文件识别语音?应用程序必须识别来自麦克风的语音。您的logcat与您发布的代码不匹配。在日志中切换到名为“wakeup”的搜索,而在代码中根本没有这样的搜索。您可能还需要粘贴相关代码。@NikolayShmyrev,我编辑了代码。谢谢你的宝贵意见
String caption = getResources().getString(captions.get(searchName));
((TextView) findViewById(R.id.caption_text)).setText(caption);