Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Dylan Merritt
15,682 PointsI'm building an app and attempting to implement text to speech. My app crashes no matter what I try.
As the title states, I am building a personal app and trying to implement text to speech. This is my first app and this may be a little ambitious but I really want it to work. The app displays a list of Russian proverbs, along with their English counterpart. Ultimately I would like the app to read the Russian text aloud so that the app can be useful to users who do not speak Russian, but I am having massive amounts of trouble with this. No matter what I try, my app crashes when it starts in the emulator. If someone could take a look at my code and try to give me advice on what I'm doing wrong and what I need to do differently, I would appreciate that so much. Here is my code for anyone kind enough to take a look for me. I know that it's messy right now. I have gone through every tutorial I can find, with no luck, and this has resulted in some code that confuses me when I look at it. I am in way over my head here. Hopefully one of you can help me out.
7 Answers
Ben Jakuben
Treehouse TeacherWhen your app crashes some errors will show up in the logcat view in Android Studio or Eclipse. Can you paste in the errors from logcat? Paste as much as you think is needed. The errors will be highlighted in red, so they are usually easy to find.
Dylan Merritt
15,682 PointsHi Ben, thank you so much for responding! Can I just say that I am so grateful for the work you do here at Treehouse. I even used the Fun Facts course as a sort of template for this app. Here is the output from my LogCat, starting from the first error. Hopefully this can be of use. I've been going insane trying to make this work.
Dylan Merritt
15,682 PointsRyan Farber
5,329 PointsIf I had to take a stab, I'm willing to bet it's this line
private void convertTextToSpeech() {
String text = toBeSpoken.getText().toString();
if (null == text || "".equals(text)) {
text = "Please give some input.";
}
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, null);
}
Dylan Merritt
15,682 PointsWhat do you suggest I change? I know it isn't reading what I need it to read, but it's sort of a placeholder that I'm using just until I can get the text to speech to work. Is there anything inherently wrong with this line?
Ryan Farber
5,329 Pointsif (null == text || "".equals(text)) { text = "Please give some input."; }
Is the main issue with the line. Change it to a toast line instead, then let me know if it still crashes
Dylan Merritt
15,682 PointsUnfortunately that did not fix the issue.
Ryan Farber
5,329 PointsWhat do you have now for a logcat? Preferably a picture with red in it :)
Dylan Merritt
15,682 PointsHere it is. Hope this helps.
Ryan Farber
5,329 PointsWell, I can't really do too much, but I can give you some sources and ideas
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo
This has to do with the android manifest. You forgot to add an activity
java.lang.NullPointerException
The original one i tried to solve. Somewhere you have a null that is not initialized
From the look of the logcat, can you post your code for the following:
SpeakPhrase.java - line 16
RussianPhraseActivity - line 19
To understand this error
http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it
Dylan Merritt
15,682 PointsHere are the lines you requested. I don't really understand why those would be null.
private SpeakPhrase mSpeakPhrase = new SpeakPhrase();
public class SpeakPhrase extends Activity implements TextToSpeech.OnInitListener
Ryan Farber
5,329 PointsThis is too hard for me to debug without all the materials in front of me. I enjoy debugging, but not when it's a huge guessing game.
I know its probably a pain in the ass, but if I am going to continue, I need all the files so I can debug it all at once, preferably on GitHub so I don't have to deal with the hassle of copy /paste and looking at a bunch of different pictures.
If not, that's fine. I'm going to throw some final comments out.
You didn't list which line came from which file, but I am guessing if you followed the Fun Facts template, there is an error in the file you linked with the private class and how you set up that page.
Also what would happen if you removed the 'TextToSpeech.' ?
Sorry I couldn't be more help. The errors are coming when those two lines are referenced, so you have to see how they are referenced if you want a clue into solving this issue.
If I were in your shoes and really wanted to finish this personal project, I would just slowly go through each line and make a comment on what it does, what it references, and hopefully you can figure it out before you do all the lines of code.
Good luck.
Dylan Merritt
15,682 PointsThanks for the help. I think I will do that, and hopefully Ben will respond with some more insight before I get too far into the commenting process. Sorry for being frustrating. I am still very much a beginner to all of this.
Ryan Farber
5,329 PointsYou learn the most from debugging. Just keep at it :)
cbcbcb
16,560 PointsCan you please change the access modifiers for these two member fields to protected
private PhraseBook mPhraseBook = new PhraseBook();
private ColorWheel mColorWheel = new ColorWheel();
Also can you move the intialization of the two fields inside of the onCreate() method and change the declarations to assign null pointers like so
protected PhraseBook mPhraseBook = null;
protected ColorWheel mColorWheel = null;
...
... onCreate()
{
...
mPhraseBook = new mPhraseBook();
mColorBook = new mColorBook();
....
}
Add override annotation for the onPause method and include the super's call to the method and remove the system call to exit and replace with a logging message like so:
@Override
public void onPause()
{
super.onPause();
Log.d(getClass().getSimpleName(), "onPause() called");
}
Also change your pojo classes to be PODO classes instead and get rid of all code outside of the main activity that references your EditText/TextView objects that are declared in your activity classes as the encapsulation is not necessary and has high overhead in the Dalvik as opposed to the JVM.
public class PhraseBook
{
public String phrase = null;
}
Also please copy any paste your android application's manifest.xml file in your reply.
Please give me feedback after doing this.
Dylan Merritt
15,682 PointsOkay, I believe that I have correctly followed all of the steps you requested. Unfortunately this does not seem to have fixed the issue, however my NullPointerException error is gone and all that remains is a StackOverflowError. Here is my AndroidManifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nalydttirrem.russianphrases" >
<application
android:allowBackup="true"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.nalydttirrem.russianphrases.RussianPhrasesActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.nalydttirrem.russianphrases.SpeakPhrase" >
</activity>
</application>
</manifest>
Thank you so much for taking the time to help. I am slightly confused on your last direction, so hopefully I did it properly.