As Dhruvisha and load , the NullPointerException exception is obvious because onCreate
is executed before onActivityResult
. My suggestion is to cast all your code that uses tts
to the procedure after checking. Here is my hint code:
@Override public void onCreate(Bundle me) { super.onCreate(me); setContentView(R.layout.examrules); Intent checkIntent = new Intent(); checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == MY_DATA_CHECK_CODE) { if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) { // success, create the TTS instance tts = new TextToSpeech(this, this); this.speak(); } else { // missing data, install it Intent installIntent = new Intent(); installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); //tts.isLanguageAvailable(Locale.INDIA_HINDI); startActivity(installIntent); } } } @Override public void onInit(int status) { if (status == TextToSpeech.SUCCESS) { // tts.setLanguage(Locale.US); Locale loc = new Locale ("IN", "en"); tts.setLanguage(loc); Toast.makeText(ExamRulesActivity.this, "Text-To-Speech engine is initialized", Toast.LENGTH_LONG).show(); } else if (status == TextToSpeech.ERROR) { Toast.makeText(ExamRulesActivity.this, "Error occurred while initializing Text-To-Speech engine", Toast.LENGTH_LONG).show(); } } public void speak() { int isVoiceEnabled=bundle.getInt("isVoiceEnabled"); setResult(RESULT_OK, intent); if(isVoiceEnabled==1) { String ruleOne="hi."; String ruleTwo= "How are you"; String ruleThree= "will you meet e=me?"; String ruleFour= " No,Ok"; if (ruleOne!=null && ruleOne.length()>0) { tts.speak(ruleOne, TextToSpeech.QUEUE_ADD, null); tts.speak(ruleTwo, TextToSpeech.QUEUE_ADD, null); tts.speak(ruleThree, TextToSpeech.QUEUE_ADD, null); tts.speak(ruleFour, TextToSpeech.QUEUE_ADD, null); } } } }
source share