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

Android Android Data Persistence Key-Value Saving with SharedPreferences Reading a Value from SharedPreferences

Reading a value from SharedPreferences Code Challenge help. I really tried hard and I can not seem to get it, help?

I have been working on this for 30 minutes and I am really stuck. I am not exactly sure what to do.

CodeChallenge.java
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

int answer = getInt("MeaningOfLife", hello);

1 Answer

You are pretty close...first of all you can't give hello as a value, as getInt wants an "int" as the 2nd argument. Lastly it tells you that... " Get the answer you just stored in SharedPreferences using the key "MeaningOfLife"!"

so...your last line of code for example, needs to be tweaked as..

int answer = mSharedPreferences.getInt("MeaningOfLife", 7);

Thank you for your support.