簡體   English   中英

NullPointerException on SharedPreferences(似乎是上下文)

[英]NullPointerException on SharedPreferences (Seems to be the context)

我剛開始使用Android Studio,目前正在嘗試制作一個簡單的應用程序,讓您做筆記並保存筆記,也許還有一些“待辦事項”功能。 我正在嘗試使用Json / Gson將它們存儲在SharedPreferences中。 遵循了一些在線教程,但似乎無法使其正常工作。 這里類似的問題也沒有幫助。 到目前為止,這是我的代碼:

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        NoteHandler noteHandler = new NoteHandler();
        noteHandler.newNote("test", 1);
    }

}

Note.java:

public class Note {

    public enum State { TODO, DONE };

    private String content;
    private State state;
    private Date date;
    private int id;
    private Note parent;

    public Note(String content, State state, Date date, Note parent, int id) {
        this.content = content;
        this.state = state;
        this.date = date;
        this.parent = parent;
        this.id = id;
    }

    /* Getters and setters */

}

NoteHandler.java:

public class NoteHandler {

    NotePrefs notePrefs = new NotePrefs(ContextGetter.getAppContext());

    void newNote(String content, int id) {
        newNote(content, null, id);
    }

    void newNote(String content, Note parent, int id) {
        Note newNote = new Note(content, Note.State.TODO, new Date(), parent, id);
        notePrefs.saveNote(newNote);
    }

    Note getNote(int id) {
        return notePrefs.loadNote(id);
    }
}

NotePrefs.java:

public class NotePrefs {
    private final String PREFS_NAME = "notes.NotePrefs";
    private static SharedPreferences settings;
    private static SharedPreferences.Editor editor;
    private static Gson gson = new Gson();

    public NotePrefs(Context ctx) {
        if(settings == null) {
            settings = ctx.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        }

        editor = settings.edit();
    }

    public static void saveNote(Note note) {
        editor.putString("" + note.getId(), gson.toJson(note));
        editor.apply();
    }

    public static Note loadNote(int id) {
        String noteJson = settings.getString("" + id, "");
        return gson.fromJson(noteJson, Note.class);
    }

    public static List<Note> loadAllNotes() {
        return null;
    }
}

ContextGetter.java:

public class ContextGetter extends Application {

    private static Context context;

    public void onCreate(){
        super.onCreate();
        context = getApplicationContext();
    }

    public static Context getAppContext() {
        return context;
    }
}

例外:

java.lang.RuntimeException:無法啟動活動ComponentInfo {notes / notes.MainActivity}:java.lang.NullPointerException:嘗試調用虛擬方法'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)'在空對象引用上

似乎仍然無法將我的腦袋纏在上下文中。 任何幫助表示贊賞!

另外,什么是實現loadAllNotes()的好方法?

干杯

您應該添加name=".ContextGetter"以在<application>標記中顯示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM