我将语音函数放入一个名为voiceAssistants的新类中,但是,当我启动它时,它总是给我一个空指针异常错误。
语音助理:public class VoiceAssistants {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private static VoiceActivityHelper voiceAssistants;
public VoiceAssistants(){
voiceAssistants = new VoiceActivityHelper();
}
/**
* Start voice input
*/
public void starVoiceAssistants(){
this.voiceAssistants.startVoiceRecognitionActivity();
}
private static class VoiceActivityHelper extends Activity {
private static ArrayList<String> matches;
public ArrayList<String> getResultSet(){
return matches;
}
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, String.format(
"%s_%s", Locale.getDefault().getLanguage(), Locale
.getDefault().getCountry()));
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
"Speech recognition demo");
//error happen here!!!
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
}
}
但是当我将它放入由新项目生成的正常活动时,它不会出现任何错误。 记录信息:
E/AndroidRuntime(22769): java.lang.NullPointerException
E/AndroidRuntime(22769):
at android.app.Activity.startActivityForResult(Activity.java:2827)
E/AndroidRuntime(22769):
at panda.com.db.VoiceAssistants$VoiceActivityHelper.startVoiceRecognitionActivity(VoiceAssistants.java:56)
E/AndroidRuntime(22769):
at panda.com.db.VoiceAssistants$VoiceActivityHelper.access$1(VoiceAssistants.java:47)
E/AndroidRuntime(22769):
at panda.com.db.VoiceAssistants.starVoiceAssistants(VoiceAssistants.java:24)
请帮忙,谢谢!!
答案 0 :(得分:1)
您正在呼叫startActivityForResult()
,但这不是Activity
。您的Activity
是一个私人内部类,没有其他人可以访问。