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

Tina Maddox
Tina Maddox
28,102 Points

SharedPreferencesApp crashing! Please help and Happy Thanksgiving :)

package com.codelouisville.tinabryan.sharedpreferencesapp;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private static final String PREFS_FILE = "com.codelouisville.tinabryan.sharedpreferencesapp";
    private static final String KEY_EDITTEXT = "key_edittext";
    private SharedPreferences.Editor mEditor;
    private SharedPreferences mSharedPreferences;
    private EditText mEditText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mEditText = (EditText) findViewById(R.id.editText);
        mSharedPreferences = getSharedPreferences(PREFS_FILE, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();

        String editTextString  = mSharedPreferences.getString(KEY_EDITTEXT, "");
        mEditText.setText(editTextString);
    }

    @Override
    protected void onPause() {
        super.onPause();

        mEditor.putString(KEY_EDITTEXT, mEditText.getText().toString());
        mEditor.apply();

    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/editText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

5 Answers

Kevin Faust
Kevin Faust
15,353 Points

Hi Tina,

1) the person you tagged doesn't seem to be an active member on treehouse

2) I inputted your code into a blank project, and I see what could be potentially your problem. In your design, you see you have a TextView right? But in your main code you wrote this:

mEditText = (EditText) findViewById(R.id.editText);

I think you might have accidentally used a textview instead of an edittext :)

Tell me if that fixes your problem!!

Happy Thanksgiving

Tina Maddox
Tina Maddox
28,102 Points

OH MY GOODNESS KEV, I FIGURED IT OUT!!!!!!!

It WORKS!

I cannot thank you enough!!!!!!!!!

God Bless you for helping me =D <3

Kevin Faust
Kevin Faust
15,353 Points

No Problem. Glad you got it fixed !!

Tina Maddox
Tina Maddox
28,102 Points

Kevin Fairley Please, Can you help with this? Thank you!

Tina Maddox
Tina Maddox
28,102 Points

Thank you Kevin! I Please pardon my ignorance but I do not know how to check or change that.

Tina Maddox
Tina Maddox
28,102 Points

Would I change in in the activity_mail.xml? or Refactor?