将数据保存到共享首选项时出现空指针异常

时间:2014-02-17 12:52:15

标签: android android-asynctask nullpointerexception sharedpreferences

我将数据保存到asynctask内的共享首选项。在这个asynctask我从服务器下载文件。如果我从服务器下载多个文件我的第一个文件下载完成时我的第二个文件启动我得到nullPointer异常

我正在使用片段。我在活动A中下载文件,我的下载正在进行中我正在更改下载文件之间的活动。我在活动B我的文件下载完成第二个文件从这里开始我得到空指针异常。为什么呢?

我试过这个:

流程1:

MyPreferences.saveDownloadingStatusIntoPreferences(
                getActivity().getApplicationContext(), "downloading",
                "process"); 

和 过程2:

MyPreferences.saveDownloadingStatusIntoPreferences(
                MyFragment.this.getActivity(), "downloading",
                "process");

流程3:

 MyPreferences.saveDownloadingStatusIntoPreferences(
                getActivity(), "downloading",
                "process");

这个方法是我在onProgressUpdate中使用。我正在使用进程2& 3我的上下文为null我得到nullpointerexception.And我使用进程1我的上下文不是null但我得到空指针异常。

我的偏好方法:

 public static void saveDownloadingStatusIntoPreferences(Context context, String key, String value) {
    System.out.println("context : "+context+" key : "+key +" value : "+value);
    SharedPreferences sharedPreferences = context.getSharedPreferences(
            MY_ST_PREFS, Context.MODE_PRIVATE);
    Editor editor = sharedPreferences.edit();
    editor.putString(key, value);
    editor.commit();
} 

为什么我得到空指针异常任何人帮助我。

编辑#1:

我的logcat

02-17 12:57:47.352: E/AndroidRuntime(2207): FATAL EXCEPTION: main
02-17 12:57:47.352: E/AndroidRuntime(2207): java.lang.NullPointerException
02-17 12:57:47.352: E/AndroidRuntime(2207): at info.app.activity.FragmentActivity$DownloadFileFromURL.onProgressUpdate(FragmentActivity.java:934)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at  info.app.activity.FragmentActivity$DownloadFileFromURL.onProgressUpdate(FragmentActivity.java:1)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:647)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at android.os.Looper.loop(Looper.java:137)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at java.lang.reflect.Method.invokeNative(Native Method)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at java.lang.reflect.Method.invoke(Method.java:511)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at dalvik.system.NativeStart.main(Native Method)

我在使用Process 1时遇到此异常。如果我使用Process 2& 3这个例外我正在获取

02-17 12:57:47.352: E/AndroidRuntime(2207): java.lang.NullPointerException
02-17 12:57:47.352: E/AndroidRuntime(2207): at info.app.preference.MyPreference.saveDownloadingStatusIntoPreferences(MyPreference.java:934)
02-17 12:57:47.352: E/AndroidRuntime(2207): at info.app.activity.FragmentActivity$DownloadFileFromURL.onProgressUpdate(FragmentActivity.java:934)
02-17 12:57:47.352: E/AndroidRuntime(2207):     at  info.app.activity.FragmentActivity$DownloadFileFromURL.onProgressUpdate(FragmentActivity.java:1)

这在saveDownloadingStatusIntoPreferences()

中给出了异常
SharedPreferences sharedPreferences = context.getSharedPreferences(
            MY_ST_PREFS, Context.MODE_PRIVATE);

2 个答案:

答案 0 :(得分:0)

只需使用:

要获取共享首选项,请在您的活动中使用以下方法:

SharedPreferences prefs = this.getSharedPreferences(       “com.example.app”,Context.MODE_PRIVATE); 阅读偏好:

String dateTimeKey =“com.example.app.datetime”;

//使用新的Date()使用默认值 long l = prefs.getLong(dateTimeKey,new Date()。getTime()); 编辑和保存首选项

日期dt = getSomeDate(); prefs.edit()。putLong(dateTimeKey,dt.getTime())。commit(); android sdk的示例目录包含检索和存储共享首选项的示例。它位于:

/ samples / android- / ApiDemos目录

答案 1 :(得分:0)

存储值:

 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  SharedPreferences.Editor editor = preferences.edit();
  editor.putString("Name","Harneet");
  editor.commit();

Foe检索值:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
  String name = preferences.getString("Name","");
  if(!name.equalsIgnoreCase(""))
  {
    name = name+"  Sethi";  /* Edit the value here*/
  }