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


App keeps on crashing UPDATED HELP?

Right so ive now got my code to all compile but it will keep on crashing anyone got any idea why?

package com.example.counter;

import android.app.Activity; import android.content.SharedPreferences;

public class MainActivity extends Activity {

// Private member field to keep track of the count private int mCount = 0;

public static final String PREFS_NAME = "com.example.myApp.mCount"; private SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); private SharedPreferences.Editor editor = settings.edit();

@Override public void onPause() { super.onPause(); // Always call the superclass method first mCount = settings.getInt("mCount", 0); }

@Override public void onResume() { super.onResume(); // Always call the superclass method first editor.putInt("mCount", mCount); editor.commit(); } }

3 Answers

Ben Jakuben
Ben Jakuben
Treehouse Teacher

The easiest way to save data like that in Android is to use something called SharedPreferences. Here's the documentation guide on how to use them: http://developer.android.com/guide/topics/data/data-storage.html#pref

Also, if you're interested, here are two videos on storing and retrieving data with SharedPreferences. We will definitely cover this sometime soon in Treehouse projects, too:

just updated this ive got code in there now and it crashes :(

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi @James,

Please post this as a new question as we want to maintain the history of questions and answers so that they can help people with similar problems in the future. If you can, maybe even re-edit this back to its original form. Thanks!