Search code examples
javaandroidsharedpreferencesandroid-preferencesedittextpreference

text from EdittextPreference


My EditTextPreference is this:

<EditTextPreference
    android:title="Name"
    android:summary="namepreferences"
    android:inputType="text"
    android:dialogTitle="name"
    />

in my PreferencesActivity:

namePref = (EditTextPreference)getPreferenceManager().findPreference("namepreferences");

well so far no problem.. now, i have a service with a notification. My goal is pass the namePref value in the Title of the notification.. I wrote this in the service:

SharedPreferences sp = PreferenceManager.getDefaultPreferences(this);
String name;

@Override 
  public void onCreate() {
    name = sp.getText("namepreferences", "NA");

  }

and i insert name in the title of notification but the app crashes sayng that name is null.. I can't solve..


Solution

  • Change to

    SharedPreferences sp;
    String name;
    
    @Override
    public void onCreate() {
        super.onCreate();
        sp = PreferenceManager.getDefaultPreferences(getApplicationContext());
        name = sp.getText("namepreferences", "NA");
    }