1

I'm trying to take string from user and use it later when the app is closed..now it works just when the app is in background but i lose the string when i close the app..is there a way to do it like this or i have to use SharedPreference and if i have to use it please explain how because i tried and failed..thanks alot.

this is my code in my MainActivity to the string from the EditText

public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private static String reminder;
 private EditText et;
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    // initialize variables


    sharedPreferences = getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sharedPreferences.edit();
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            et = (EditText) findViewById(R.id.Name);
            reminder = et.getText().toString();
            if(reminder == null){
                reminder = "TWEAK!";
            }

            editor.putString("TAG",reminder);
            editor.commit();
            // do stuff
}

// get the user's string
public String getRem() {
    reminder = sharedPreferences.getString("TAG", "");
    return reminder;
}

the app crashes and gives

"Attempt to invoke interface method 'java.lang.String android.content.SharedPreferences.getString(java.lang.String, java.lang.String)' on a null object reference"

at this line

reminder = sharedPreferences.getString("TAG", "");

this is the class where i call the method

public class Notifications extends BroadcastReceiver {
private String rem;

      // set notification
@Override
public void onReceive(Context context, Intent intent) {
    // object to access MainActivity methods
    MainActivity main = new MainActivity();
    rem = main.getRem();
}
  • Can you show the whole class(es) as a [mcve], not just these small samples of code? `sharedPreferences` is null because it is not initialized somehow. – OneCricketeer Nov 09 '16 at 00:06
  • i added my whole MainActivity class..i just was posting snippets related to my question – Ahmed Fahmy Nov 09 '16 at 00:14
  • Don't add the whole thing, read that link, please. Not all that code you added is related to the SharedPreferences. – OneCricketeer Nov 09 '16 at 00:25

2 Answers2

1

You can use

SharedPreferences sharedpreferences = getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString("TAG",reminder);
editor.commit();

to retrive it:

sharedpreferences.getString("TAG","");

[update]

public class Notifications extends BroadcastReceiver { 
private String rem;
 // set notification
 @Override
 public void onReceive(Context context, Intent intent) { 
// object to access MainActivity methods
 SharedPreferences sharedPreferences = context.getSharedPreferences("MyPREFERENCES",Context.MODE_PR‌​IVATE);
  rem = sharedPreferences.getString("TAG", ""); 
}

more about SharedPreferences

Tefa
  • 328
  • 1
  • 4
  • 15
  • i tried it and the app still crashes..i added the new code..what am i doing wrong? – Ahmed Fahmy Nov 09 '16 at 00:02
  • public String getRem() { reminder = ""; reminder = sharedPreferences.getString("TAG", ""); return reminder; } – Tefa Nov 09 '16 at 00:08
  • if it's not working please put the whole code for this activity. – Tefa Nov 09 '16 at 00:12
  • 1
    I see that you declare gerRem() method but you did not use it :] how the app crash?? – Tefa Nov 09 '16 at 00:25
  • i'm calling it in another class that extends BroadcastReciever..i added it – Ahmed Fahmy Nov 09 '16 at 00:31
  • public class Notifications extends BroadcastReceiver { private String rem; // set notification @Override public void onReceive(Context context, Intent intent) { // object to access MainActivity methods SharedPreferences sharedPreferences = context.getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE); String rem = sharedPreferences.getString("TAG", ""); } – Tefa Nov 09 '16 at 00:36
  • it's so close the other answer above but when i add MODE_PRIVATE inside onReceive method it turns red..the other answer gets it right..but nonetheless man you are amazing thank you so much for your patience and your time i'm just a beginner and having lot's of hard time so you helped a lot..thanks again :) – Ahmed Fahmy Nov 09 '16 at 00:51
  • Thanks :D, We rabna m3ak :D – Tefa Nov 09 '16 at 01:17
1

Wherever you called the method of getRem(), you can't do that outside the Activity as the SharedPreferences are null.

Like, I assume you made a new MainActivity(), then called getRem() on that, perhaps?

You need to obtain the SharedPreferences again from an available Context, and then you can use getString("TAG", "")

EDIT Borrowed from Shared preferences inside broadcastreceiver

public class Notifications extends BroadcastReceiver {

    private String rem;

    // set notification
    @Override
    public void onReceive(Context context, Intent intent) {
        setRem(context);
    }

    private void setRem(Context context) {
        SharedPreferences prefs = context.getSharedPreferences("MyPREFERENCES",Context.MODE_PRIVATE);
        rem = prefs.getString("TAG", "");
    }

}
Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245