如何解决SharedPreference中的NullPointerException?

时间:2017-05-01 02:52:20

标签: java android nullpointerexception sharedpreferences

我在Activity中设置了共享首选项:

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

并在片段中获取首选项

  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);

但我得到NullPointerException,这里是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)

在这一行:

 if (getActivity().getSharedPreferences("your_prefs", Activity.MODE_PRIVATE) 
 != null) {

1 个答案:

答案 0 :(得分:1)

最可能的原因是因为片段尚未附加到活动。您需要在片段的getActivity()方法中调用onActivityCreated()

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