Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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();