检查ssharedpreference中是否存在列表项

时间:2016-01-16 11:32:53

标签: android listview sharedpreferences

在我的应用程序中,我使用ssharedpreference在单击按钮时保存一些列表项并将这些项检索到另一个活动中。在其他活动中,我想检查列表项是否存在于ssharedpreference中,因此我创建了一个要检查的方法。 现在,如果我运行该应用程序,它会崩溃,在logcat中显示nullpointerexception。

检查ssharedpreference的方法

CodeList codes = (CodeList) getrrItem(position);

private boolean checkArchivedItem(CodeList checkCodes) {
                    boolean check = false;
                    List<CodeList> archives = archvPrefrnces.getArchives(InterActivity.this);
                    if (archives != null) {
                        for (CodeList codes : archives) {
                            if (codes.equals(checkCodes)) {
                                check = true;
                                break;
                            }
                        }
                    }
                    return check;
                }       

这是logcat中提到的行

List<CodeList> archives = archvPrefrnces.getArchives(InterActivity.this);

我的共享偏好

public class ArchivePreferences
{
public static final String PREFS_NAME = "CODE_ARCHIVES";
public static final String ARCHIVED = "code_archived";

public ArchivePreferences(){
    super();
}

public void saveArchives(Context context, List<CodeList> archives){
    SharedPreferences settings;
    Editor editor;

    settings = context.getSharedPreferences(PREFS_NAME,
                                            Context.MODE_PRIVATE);
    editor = settings.edit();

    Gson gson = new Gson();
    String jsonArchives = gson.toJson(archives);

    editor.putString(ARCHIVED, jsonArchives);

    editor.commit();
}

public void addArchive(Context context, CodeList code){
    List<CodeList> archives = getArchives(context);

    if(archives == null)
        archives = new ArrayList<CodeList>();
    archives.add(code);
    saveArchives(context,archives);
}

public void removeArchive(Context context, CodeList code) {
    ArrayList<CodeList> archives = getArchives(context);
    if (archives != null) {
        archives.remove(code);
        saveArchives(context, archives);
    }
}


public ArrayList<CodeList> getArchives(Context context) {
    SharedPreferences settings;
    List<CodeList> archives;
    //ArrayList<CodeList> favorites;

    settings = context.getSharedPreferences(PREFS_NAME,
                                            Context.MODE_PRIVATE);

    if (settings.contains(ARCHIVED)) {
        String jsonArchives = settings.getString(ARCHIVED, null);
        Gson gson = new Gson();
        CodeList[] archiveItems = gson.fromJson(jsonArchives,
                                                 CodeList[].class);

        archives = Arrays.asList(archiveItems);
        archives = new ArrayList<CodeList>(archives);


    } else
        return null;

    return (ArrayList<CodeList>) archives;
    //return favorites;
}
}

logcat的

01-16 16:38:32.395 22165 22165 D   AndroidRuntime                               Shutting down VM
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               FATAL EXCEPTION: main
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               Process: com.enlightenme.pac, PID: 22165
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList com.enlightenme.pac.ArchivePreferences.getArchives(android.content.Context)' on a null object reference
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at com.enlightenme.pac.InterActivity$RemoteDataTask$100000001.checkArchivedItem(InterActivity.java:323)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at com.enlightenme.pac.InterActivity$RemoteDataTask$100000001.onMenuItemClick(InterActivity.java:301)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at com.baoyz.swipemenulistview.SwipeMenuListView$100000000.onItemClick(SwipeMenuListView.java:78)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at com.baoyz.swipemenulistview.SwipeMenuView.onClick(SwipeMenuView.java:85)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at android.view.View.performClick(View.java:4780)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at android.view.View$PerformClick.run(View.java:19866)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at android.os.Handler.handleCallback(Handler.java:739)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at android.os.Handler.dispatchMessage(Handler.java:95)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at android.os.Looper.loop(Looper.java:135)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at android.app.ActivityThread.main(ActivityThread.java:5254)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Native Method)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at java.lang.reflect.Method.invoke(Method.java:372)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
01-16 16:38:32.402 22165 22165 E   AndroidRuntime                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

1 个答案:

答案 0 :(得分:1)

难道你错过了那里的第二部分吗?

ArchivePreferences archvPrefrnces = new ArchivePreferences();