-6

I have set shared preferences in Activity:

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
            SharedPreferences.Editor editor = sp.edit();
            editor.putInt("your_int_key", 14);
            editor.commit();

and in get preferences in Fragment

  if (getActivity().getSharedPreferences("your_prefs", Activity.MODE_PRIVATE) != null) {
                SharedPreferences sp = getActivity().getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
                myIntValue = sp.getInt("your_int_key", -1);
            }
            textTv[i] = (TextView) v.findViewById(nizTekstaID[i]);
            textTv[i].setVisibility(View.VISIBLE);
            textTv[i].setText(result.get(i));
            textTv[i].setTextSize(myIntValue);

but i get NullPointerException, here is Logcat:

05-01 04:46:01.298 10968-10968/com.example.bolt.automagazin E/AndroidRuntime: FATAL EXCEPTION: main
                                                                          Process: com.example.bolt.automagazin, PID: 10968
                                                                          java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.support.v4.app.FragmentActivity.getSharedPreferences(java.lang.String, int)' on a null object reference
                                                                              at com.example.bolt.automagazin.FragmentGdeSeUcitavajuVestiScroll1$asyncTekstovi.onPostExecute(FragmentGdeSeUcitavajuVestiScroll1.java:988)
                                                                              at com.example.bolt.automagazin.FragmentGdeSeUcitavajuVestiScroll1$asyncTekstovi.onPostExecute(FragmentGdeSeUcitavajuVestiScroll1.java:829)
                                                                              at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                              at android.os.AsyncTask.-wrap1(AsyncTask.java)
                                                                              at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                              at android.os.Looper.loop(Looper.java:148)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

in this line:

 if (getActivity().getSharedPreferences("your_prefs", Activity.MODE_PRIVATE) 
 != null) {
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Nikola Božić
  • 127
  • 1
  • 3
  • 12
  • So which one is line 988 of FragmentGdeSeUcitavajuVestiScroll1.java? – Dawood ibn Kareem May 01 '17 at 02:53
  • 1
    The Activity is null. Not the SharedPreferences – OneCricketeer May 01 '17 at 02:54
  • And you can (and should) get your preferences outside of an Asynctask – OneCricketeer May 01 '17 at 02:55
  • So this hapens if i use preferences, and didnt call that Activity before that, any suggestions how to resolve that? – Nikola Božić May 01 '17 at 02:57
  • add your full fragment code, `getActivity()` returns null, you must call it in a wrong place or maybe simply call it like this: `YourFragmentClassName.this.getActivity()` – Atef Hares May 01 '17 at 02:57
  • @Atef Adding `YourFragmentClassName.this` doesn't do anything to fix the Activity from being null – OneCricketeer May 01 '17 at 03:12
  • @cricket_007 Yes you are right!, I was thinking of the case that he might be calling it from an inner class or something inside the fragment but I forgot that even if this is the case then `getActivity()` will not be defined/exist. it's my mistake! – Atef Hares May 01 '17 at 03:16
  • on which method of fragment you are calling getActivity().getSharedPreferences("your_prefs", Activity.MODE_PRIVATE) != null)? – VikasGoyal May 01 '17 at 03:49
  • Better call getSharedPreferences after onActivityCreated() is done. As per Android docs: Caution: If you need a Context object within your Fragment, you can call getActivity(). However, be careful to call getActivity() only when the fragment is attached to an activity. When the fragment is not yet attached, or was detached during the end of its lifecycle, getActivity() will return null. – Yogesh May 01 '17 at 06:54

1 Answers1

1

The most likely cause is because the fragment has not yet attached to the activity. You need to call getActivity() in the fragment's onActivityCreated() method.

void onActivityCreated (Bundle savedInstanceState){ //call getActivity() here }