Text to Speak for Android not working - java

I'm trying to implement Text to Speech onto my app. I've followed the following guide but my app keeps crashing after the splash screen.
link: http://www.androidhive.info/2012/01/android-text-to-speech-tutorial/
Main Activity Class I have this
String alertMessage = AppResources.ALERT_MSG;
SpeakDemo speak = new SpeakDemo(null, alertMessage);
speak.speakOut();
If I remove the speak.speakOut(); line the app works fine.
App Resources is another class with the following message box
public static String ALERT_MSG = "Welcome!";
The SpeakDemo Class is:
import java.util.Locale;
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.util.Log;
public class SpeakDemo implements TextToSpeech.OnInitListener{
private TextToSpeech tts;
private Context context;
private String message;
public SpeakDemo(Context context, String message){
this.context = context;
this.message = message;
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
public void speakOut(){
tts.speak(message, TextToSpeech.QUEUE_FLUSH, null);
}
}
Any Ideas?
Thanks

You need to instantiate the TextToSpeech.
Change
public SpeakDemo(Context context, String message){
this.context = context;
this.message = message;
tts = new TextToSpeech(context, this);
}

//FOR TEXT TO SPEECH
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
String msg=text.getText().toString();
if(status==TextToSpeech.SUCCESS)
{
tts.setLanguage(Locale.US);
tts.speak(msg,TextToSpeech.QUEUE_FLUSH,null);
}
else
Log.e("TTS","INITILIZATION FAILED");
}
public void onDestroy()
{
if (tts!=null)
{
tts.stop();
tts.shutdown();
}
super.onDestroy();
}

Xml:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Speak"
android:onClick="TTS"/>
Mainactivity:
TextToSpeech textToSpeech;
textToSpeech=new TextToSpeech(TTSpeech.this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if(status==TextToSpeech.SUCCESS)
{
result=textToSpeech.setLanguage(Locale.ENGLISH);
}
else
{
Toast.makeText(TTSpeech.this, "speech not work", Toast.LENGTH_SHORT).show();
}
}
});
}
public void TTS(View view) {
switch (view.getId())
{
case R.id.button:
if(result== TextToSpeech.LANG_NOT_SUPPORTED || result==TextToSpeech.LANG_MISSING_DATA)
{
Toast.makeText(TTSpeech.this, "speech not nt work", Toast.LENGTH_SHORT).show();
}
else
{
text=ed.getText().toString();
textToSpeech.speak(text,TextToSpeech.QUEUE_FLUSH,null);
}
break;
}
if you get problem you can comment me.

Related

Text to speech for multiple activities

I'm developing an application for workout with multiple activities that has text to speech for instructions.
My problem is that the text to speech takes too long to initialize even though i have it in a helper class and the instruction don't play when the activity starts, i thought maybe i should make a text to speech service, but i don't know how and i didn't find any tutorials.
private TextToSpeech mTts = null;
private boolean isLoaded = false;
public void init(Context context) {
try {
mTts = new TextToSpeech(context, onInitListener);
} catch (Exception e) {
e.printStackTrace();
}
}
private final TextToSpeech.OnInitListener onInitListener = new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
mTts.setLanguage(Locale.getDefault());
isLoaded = true;
}
}
};
public void initQueue(String text) {
if (isLoaded) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null, null);
} else {
mTts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
else
Log.e("error", "TTS Not Initialized");
}
How i can create a speech service and make it speak instructions from an activity?

Text to speech playing automatically

I have a text to speech function in my android app that works to an onClick event, there is an issue when the activity starts, the text to speech starts without clicking the button, is there a line of code I can put in to stop this happening, thank you.
package com.androidhive.texttospeech;
import java.util.Locale;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AndroidTextToSpeechActivity extends Activity implements
TextToSpeech.OnInitListener {
/** Called when the activity is first created. */
private TextToSpeech tts;
private Button button1;
private TextView txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tts = new TextToSpeech(this, this);
button1 = (Button) findViewById(R.id.button1);
txtText = (TextView) findViewById(R.id.txtText);
// button on click event
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
speakOut();
}
});
}
#Override
public void onDestroy() {
// Don't forget to shutdown!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
public void onInit(int status) {
// TODO Auto-generated method stub
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.US);
// tts.setPitch(5); // set pitch level
// tts.setSpeechRate(2); // set speech speed rate
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
button1.setEnabled(true);
speakOut();
}
} else {
Log.e("TTS", "Initilization Failed");
}
}
private void speakOut() {
String text = txtText.getText().toString();
tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
}
The reason why this happens is because you invoke speakOut# In the onInit() method, remove speakOut():
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
button1.setEnabled(true);
// speakOut();
}
I saw these codes in onInit(), which will be called after the TextToSpeech is instantiated and initialized successfully. See the speakOut() here? That's what causes your problem.
if (result == TextToSpeech.LANG_MISSING_DATA
|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "Language is not supported");
} else {
button1.setEnabled(true);
speakOut();
}

Modify speech recognition without popup

I'm totally new to Android and I decided to create a Siri look-a-like app, which is working fine, I searched before asking but I can't get it to work: Android speech Recognition App Without Pop Up
So I want to hide the pop-up inside my Android app, but I really don't now how to do it properly. I'll be very glad if someone could help me doing this. I have already added the correct permissions in Android Manifest...
Here is my full source:
/**
* Giovanni Terlingen
* PWS Project
* 3 november 2014
* Original file from http://github.com/gi097/PWS
*/
package nl.giovanniterlingen.pws;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity implements OnInitListener {
private static final String TAG = "PWS";
private TextView result;
private TextToSpeech tts;
private Button speak;
private int SPEECH_REQUEST_CODE = 1234;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
speak = (Button) findViewById(R.id.bt_speak);
speak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendRecognizeIntent();
}
});
speak.setEnabled(false);
result = (TextView) findViewById(R.id.tv_result);
tts = new TextToSpeech(this, this);
}
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
speak.setEnabled(true);
} else {
// failed to init
finish();
}
}
private void sendRecognizeIntent() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Aan het luisteren...");
intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 100);
startActivityForResult(intent, SPEECH_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SPEECH_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
ArrayList<String> matches = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
if (matches.size() == 0) {
tts.speak("Ik heb niks gehoord, probeer het nog eens",
TextToSpeech.QUEUE_FLUSH, null);
} else {
String mostLikelyThingHeard = matches.get(0);
result.setText("Dit heeft u gezegd: "
+ mostLikelyThingHeard + ".");
mostLikelyThingHeard = mostLikelyThingHeard.toLowerCase();
boolean found = false;
String[] groeten = { "hallo", "heey", "hoi", "hey", "he",
"hee", "hay" };
for (String strings : groeten) {
if (mostLikelyThingHeard.contains(strings)) {
tts.speak("Hey leuk dat je er bent!",
TextToSpeech.QUEUE_FLUSH, null);
found = true;
break;
}
}
String[] okay = { "oké, oke, ok, okay, okee" };
for (String strings : okay) {
if (mostLikelyThingHeard.contains(strings)) {
tts.speak("Okiedokie!",
TextToSpeech.QUEUE_FLUSH, null);
found = true;
break;
}
}
String[] vragen = { "hoe gaat het", "hoe gaat het met je", "hoe gaat het met jou", "hoe gaat het met u", "hoe is het"};
for (String strings : vragen) {
if (mostLikelyThingHeard.contains(strings)) {
tts.speak("Met mij gaat het altijd goed, met jou?",
TextToSpeech.QUEUE_FLUSH, null);
found = true;
break;
}
}
String[] wie = { "wie ben", "hoe heet je", "hoe heet jij", "wat is je naam", "wat is uw naam"};
for (String strings : wie) {
if (mostLikelyThingHeard.contains(strings)) {
tts.speak("Ik ben PWS, ik ben gemaakt als profielwerkstuk door Giovanni Terlingen.",
TextToSpeech.QUEUE_FLUSH, null);
found = true;
break;
}
}
String[] leeftijd = { "hoe oud ben" };
for (String strings : leeftijd) {
if (mostLikelyThingHeard.contains(strings)) {
tts.speak("Ik ben op 3 november 2014 van start gegaan dus dat mag je zelf uitrekenen",
TextToSpeech.QUEUE_FLUSH, null);
found = true;
break;
}
}
String[] lachen = { "haha", "hah", "ha" };
for (String strings : okay) {
if (mostLikelyThingHeard.contains(strings)) {
tts.speak("Haha leuk grapje",
TextToSpeech.QUEUE_FLUSH, null);
found = true;
break;
}
}
if (!found) {
String doei = "doei";
if (mostLikelyThingHeard.equals(doei)) {
tts.speak("Okay tot de volgende keer!",
TextToSpeech.QUEUE_FLUSH, null);
} else {
tts.speak("Ik begrijp niet wat je bedoeld met "
+ mostLikelyThingHeard
+ " probeer het anders te verwoorden.",
TextToSpeech.QUEUE_FLUSH, null);
}
}
}
}
} else {
Log.d(TAG, "result NOT ok");
}
super.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onDestroy() {
if (tts != null) {
tts.shutdown();
}
super.onDestroy();
}
}
Step 1
In Android Manifest.xml, add the permission.
<uses-permission android:name="android.permission.RECORD_AUDIO" />
Step 2
In the Layout screen, i am using a Toggle Button to start/stop voice listening.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
.......
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="ToggleButton" />
......
</RelativeLayout>
Step 3 - Activity Class
package com.example.voice;
import java.util.ArrayList;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.ToggleButton;
public class VoiceRecognitionActivity extends Activity implements
RecognitionListener {
private TextView returnedText;
private ToggleButton toggleButton;
private SpeechRecognizer speech = null;
private Intent recognizerIntent;
private String LOG_TAG = "VoiceRecognitionActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
returnedText = (TextView) findViewById(R.id.textView1);
toggleButton = (ToggleButton) findViewById(R.id.toggleButton1);
speech = SpeechRecognizer.createSpeechRecognizer(this);
speech.setRecognitionListener(this);
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE,
"en");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,
this.getPackageName());
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 3);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
speech.startListening(recognizerIntent);
} else {
speech.stopListening();
}
}
});
}
#Override
public void onResume() {
super.onResume();
}
#Override
protected void onPause() {
super.onPause();
if (speech != null) {
speech.destroy();
Log.i(LOG_TAG, "destroy");
}
}
#Override
public void onBeginningOfSpeech() {
Log.i(LOG_TAG, "onBeginningOfSpeech");
}
#Override
public void onBufferReceived(byte[] buffer) {
Log.i(LOG_TAG, "onBufferReceived: " + buffer);
}
#Override
public void onEndOfSpeech() {
Log.i(LOG_TAG, "onEndOfSpeech");
toggleButton.setChecked(false);
}
#Override
public void onError(int errorCode) {
String errorMessage = getErrorText(errorCode);
Log.d(LOG_TAG, "FAILED " + errorMessage);
returnedText.setText(errorMessage);
toggleButton.setChecked(false);
}
#Override
public void onEvent(int arg0, Bundle arg1) {
Log.i(LOG_TAG, "onEvent");
}
#Override
public void onPartialResults(Bundle arg0) {
Log.i(LOG_TAG, "onPartialResults");
}
#Override
public void onReadyForSpeech(Bundle arg0) {
Log.i(LOG_TAG, "onReadyForSpeech");
}
#Override
public void onResults(Bundle results) {
Log.i(LOG_TAG, "onResults");
ArrayList<String> matches = results
.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
String text = "";
for (String result : matches)
text += result + "\n";
returnedText.setText(text);
}
#Override
public void onRmsChanged(float rmsdB) {
Log.i(LOG_TAG, "onRmsChanged: " + rmsdB);
}
public static String getErrorText(int errorCode) {
String message;
switch (errorCode) {
case SpeechRecognizer.ERROR_AUDIO:
message = "Audio recording error";
break;
case SpeechRecognizer.ERROR_CLIENT:
message = "Client side error";
break;
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
message = "Insufficient permissions";
break;
case SpeechRecognizer.ERROR_NETWORK:
message = "Network error";
break;
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
message = "Network timeout";
break;
case SpeechRecognizer.ERROR_NO_MATCH:
message = "No match";
break;
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
message = "RecognitionService busy";
break;
case SpeechRecognizer.ERROR_SERVER:
message = "error from server";
break;
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
message = "No speech input";
break;
default:
message = "Didn't understand, please try again.";
break;
}
return message;
}
}

TextToSpeech only works when button is pressed, not when called separately

I tried to do something extremely simple or I thought it was.
In my BluetoothChat, I set
public static boolean potato = false;
In the onCreateBundle of my MainActivity, I have
talker = new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener()
{
#Override
public void onInit(int status)
{
if(status != TextToSpeech.ERROR)
{
talker.setLanguage(Locale.US);
}
}
});
if(BluetoothChat.potato == false)
{
speakOut();
}
When speakOut(); is called by a button or separately by itself it works.
public void speakOut()
{
String original ="You will have a seizure in thirty seconds.";
talker.speak(original,TextToSpeech.QUEUE_FLUSH,null);
}
However, this does not work. Can someone explain why?
Thanks to the help from Payeli. Here is the solution! Place the if statement within the onInit.
talker = new TextToSpeech(getApplicationContext(),new TextToSpeech.OnInitListener()
{
#Override
public void onInit(int status)
{
if(status != TextToSpeech.ERROR)
{
talker.setLanguage(Locale.US);
}
if(BluetoothChat.potato == false)
{
speakOut();
}
}
});

Need help debugging: Failure delivering result - java.lang.NullPointerException

I have looked around through the other questions with the same problem, but because this error is specific to everyone's personal code, they didn't help much. I'm getting the following error:
(I can't upload images yet, and Eclipse won't let me copy+paste the error, so because I'm lazy, here's a Gyazo: CLICK ME)
Here's my code:
Mikey.java (ignore the stupid names)
package com.jamco.apps.mikey;
import java.util.ArrayList;
import java.util.Locale;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class Mikey extends Activity implements RecognitionListener, OnClickListener, TextToSpeech.OnInitListener {
protected static final int REQUEST_OK = 1;
private ImageButton btn;
private TextToSpeech tts;
private TextView txt;
private String thoughts;
private ArrayList<String> thingsYouSaid;
private Integer numberOfThingsSaid = 0;
#Override
public void onDestroy() {
//Don't forget to shutdown tts!
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onDestroy();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mikey);
btn = (ImageButton) findViewById(R.id.imageButton1);
txt = (TextView) findViewById(R.id.textView1);
tts = new TextToSpeech(this, this);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent i = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
i.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(i, REQUEST_OK);
} catch (Exception e) {
Toast.makeText(this, "Error initializing speech to text engine.", Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==REQUEST_OK && resultCode==RESULT_OK) {
numberOfThingsSaid += 1;
ArrayList<String> youJustSaid = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
((TextView)findViewById(R.id.textView1)).setText("You said: " + youJustSaid);
thingsYouSaid.add(youJustSaid.get(0).toString());
think();
}
}
#Override
public void onInit(int status) {
if (status == TextToSpeech.SUCCESS) {
int result = tts.setLanguage(Locale.UK);
if (result == TextToSpeech.LANG_MISSING_DATA || result == TextToSpeech.LANG_NOT_SUPPORTED) {
Log.e("TTS", "This Language is not supported");
} else {
btn.setEnabled(true);
}
} else {
Log.e("TTS", "Initilization Failed!");
}
}
#Override
public void onBeginningOfSpeech() {
//TODO Auto-generated method stub
}
#Override
public void onBufferReceived(byte[] buffer) {
//TODO Auto-generated method stub
}
#Override
public void onEndOfSpeech() {
//TODO Auto-generated method stub
}
#Override
public void onError(int error) {
//TODO Auto-generated method stub
}
#Override
public void onEvent(int eventType, Bundle params) {
//TODO Auto-generated method stub
}
#Override
public void onPartialResults(Bundle partialResults) {
//TODO Auto-generated method stub
}
#Override
public void onReadyForSpeech(Bundle params) {
//TODO Auto-generated method stub
}
#Override
public void onResults(Bundle results) {
//TODO Auto-generated method stub
}
#Override
public void onRmsChanged(float rmsdB) {
//TODO Auto-generated method stub
}
private void speak(String speech) {
tts.speak(speech, TextToSpeech.QUEUE_FLUSH, null);
Toast.makeText(this, speech, Toast.LENGTH_LONG).show();
}
private void think() {
if (thingsYouSaid.get(numberOfThingsSaid) == "Hello" || thingsYouSaid.get(numberOfThingsSaid) == "Hi") {
switch (randInt(0, 2)) {
case 0:
speak("Hello");
case 1:
speak("Hello");
case 2:
speak(thingsYouSaid.get(numberOfThingsSaid));
}
}
}
public static int randInt(int min, int max) {
//Usually this can be a field rather than a method variable
Random rand = new Random();
//nextInt is normally exclusive of the top value,
//so add 1 to make it inclusive
int randomNum = rand.nextInt((max - min) + 1) + min;
return randomNum;
}
}
(Sorry if the formatting is bad, I'm new to this site).
Here is my activity xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".Mikey" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="119dp"
android:text="Click the Microphone and ask a question!" />
</RelativeLayout>
The Stack-trace tells me to go to this line:
thingsYouSaid.add(youJustSaid.get(0).toString());
However, after lots of trial and error I can't work out what's wrong with it. I tried adding .toString() just to see if it magically fixed something, but no.
Hopefully someone will be able to help me solve this :)
Merry Christmas!
I don't see anywhere that you are initializing thingsYouSaid;. You declare it like this
private ArrayList<String> thingsYouSaid;
but you don't initialize it. Try changing that to
private ArrayList<String> thingsYouSaid = new ArrayList<String>();
Edit
I haven't worked with TextToSpeech but I can tell you that you are comparing Strings incorrectly here
thingsYouSaid.get(numberOfThingsSaid) == "Hello" || thingsYouSaid.get(numberOfThingsSaid) == "Hi")
inside think(). You should use equals() to compare Strings.
if ((("Hello".equals(thingsYouSaid.get(numberOfThingsSaid))
|| ("Hi".equals(thingsYouSaid.get(numberOfThingsSaid))))
also, you should add break; statements at the end of each case or the logic will fall through even if it matches one of the first case statements.
thingsYouSaid is not instantiated anywhere in your code. You should instantiate it (you can do that while declaring):
ArrayList<String> thingsYouSaid = new ArrayList<String>();

Categories