-1

I have the following code:

 public void refresh(){
        View rootView=getView();
        final SharedPreferences pref = this.getContext().getSharedPreferences("MyFav", 0);
        final SharedPreferences.Editor editor = pref.edit();
        Map<String,?> entries = pref.getAll();
        final Set<String> keys = entries.keySet();}

This function is inside a fragment and I am calling it from Activity as follows:-

public void onBackPressed() {
        super.onBackPressed();
        FavLegislator fav =new FavLegislator();
        if(frag.equals("true"))
            fav.refresh();
        finish();
    }

The shared preferences might have changed in the Activity.
I just want an updated list of preferences when the Activity closes.

I am getting the following error on the back button press of Activity:

11-27 10:06:24.126 31645-31645/com.app.congress.congressapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.congress.congressapp, PID: 31645
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
  at com.app.congress.congressapp.FavLegislator.refresh(FavLegislator.java:85)
  at com.app.congress.congressapp.legislator_info.onBackPressed(legislator_info.java:466)
  at com.app.congress.congressapp.legislator_info.onOptionsItemSelected(legislator_info.java:454)
  at android.app.Activity.onMenuItemSelected(Activity.java:2912)
  at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
  at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
  at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
  at android.support.v7.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:194)
  at android.view.View.performClick(View.java:5201)
  at android.view.View$PerformClick.run(View.java:21163)
  at android.os.Handler.handleCallback(Handler.java:746)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:148)
  at android.app.ActivityThread.main(ActivityThread.java:5443)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Can some one please let me know why I am getting the above error and how to fix it?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Zxxxxx
  • 397
  • 1
  • 5
  • 16

2 Answers2

1

super.onBackPressed(); call will destroy/finish your activity and you can no longer use any object belong to this activity so simply move this call at the end and there is no need to call finish()

public void onBackPressed() {
        FavLegislator fav =new FavLegislator();
        if(frag.equals("true"))
            fav.refresh();
        super.onBackPressed();
    }

change this

 final SharedPreferences pref = this.getContext().getSharedPreferences("MyFav", 0);

to this

final SharedPreferences pref = getActivity().getSharedPreferences("MyFav", 0);

Note : Better option is to create a utility class with a static method which will save your preference data so just pass the context and data to utility class and save it to preference

public class Util {

    public static void saveData(Context context,int i ){
      // your preference code , change parameters accordingly 
    }
}

from your activity call it like

 Util.saveData(this,5);
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
1

You cannot call new FavLegislator() and expect it to have a Context.

That appears to be an Activity or a Fragment class. When you make a new one, the Context is not attached, so getContext() returns null.

If you're trying to refresh some other view when you return to it, use the onResume method

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245