Does TextToSpeech show Null Pointer exception?

I am creating one application in TextToSpeech. But when I try to run, it throws an exception on the next line.

tts.speak(ruleOne, TextToSpeech.QUEUE_ADD, null); 

Here is my complete code for reference
Please give me a hint. I do not understand what is wrong in my code. when I compile my code, the whole value is passed correctly, but when it jumps over the tts object, it removes the null pointer exception. is any syntax wrong? which method to call onCreate () or onActivityResult first? Thanks at Advance

Here are the contents of Logcat.

 04-10 13:58:34.082: WARN/System.err(19352): java.lang.NullPointerException 04-10 13:58:34.082: WARN/System.err(19352): at com.example.examguide.ExamRulesActivity.onCreate(ExamRulesActivity.java:60) 04-10 13:58:34.082: WARN/System.err(19352): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-10 13:58:34.092: WARN/System.err(19352): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 04-10 13:58:34.092: WARN/System.err(19352): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 04-10 13:58:34.092: WARN/System.err(19352): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 04-10 13:58:34.092: WARN/System.err(19352): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 04-10 13:58:34.092: WARN/System.err(19352): at android.os.Handler.dispatchMessage(Handler.java:99) 04-10 13:58:34.092: WARN/System.err(19352): at android.os.Looper.loop(Looper.java:123) 04-10 13:58:34.092: WARN/System.err(19352): at android.app.ActivityThread.main(ActivityThread.java:3683) 04-10 13:58:34.102: WARN/System.err(19352): at java.lang.reflect.Method.invokeNative(Native Method) 04-10 13:58:34.102: WARN/System.err(19352): at java.lang.reflect.Method.invoke(Method.java:507) 04-10 13:58:34.102: WARN/System.err(19352): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 04-10 13:58:34.102: WARN/System.err(19352): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 04-10 13:58:34.102: WARN/System.err(19352): at dalvik.system.NativeStart.main(Native Method) 
+6
source share
4 answers

to write

 tts = new TextToSpeech(this, this); 

in an if loop in the onCreate () method.

+9
source

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); } } } } 
+3
source

Does not work onCreate until onActivityResult ?

So your tts object will be zero. Add extra code to check tts is not null before you call tts.speak

+2
source

You just need to copy the lines

 Intent checkIntent = new Intent(); checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); 

And write right after you wrote setContentView()

This is because before you do anything with the TextToSpeach object, you need to check if the data is installed.

Thus, the error in the code is that you wrote everything new in this example , which you wrote before calling intent .

0
source

Source: https://habr.com/ru/post/912682/


All Articles