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 Activity Lifecycle Introducing SharedPreferences Saving and Retrieving Data with SharedPreferences

Can someone help me with challenges for SharedPreferences?

I got through challenge 1 but not I can't get through challenge 2. I'm following along with the video, but I'm missing something.

Any help would be appreciated. Thanks!

Challenge 2: Now declare a SharedPreferences field named mSharedPreferences and an Editor field named mEditor. Then initialize both of these fields in the onCreate method.

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hi Ja'Nel,

It should be

  private SharedPreferences.Editor mEditor;

instead of just

  private Editor mEditor;

Secondly your forgetting to put some things inside your getSharedPreferences("code should be in here")

mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);

Do you remember that? And then we initialize the mEditor variable by calling edit() on our mSharedPreferences

 mEditor = mSharedPreferences.edit();