简体   繁体   English

NullPointerException on SharedPreferences(似乎是上下文)

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

I'm just getting started with Android Studio, currently trying to make a simple app that lets you take notes and save them, maybe some 'to do list' features. 我刚开始使用Android Studio,目前正在尝试制作一个简单的应用程序,让您做笔记并保存笔记,也许还有一些“待办事项”功能。 I'm trying to store them in SharedPreferences using Json / Gson. 我正在尝试使用Json / Gson将它们存储在SharedPreferences中。 Followed some online tutorials but can't seem to get it to work. 遵循了一些在线教程,但似乎无法使其正常工作。 Similar questions on here didn't help either. 这里类似的问题也没有帮助。 This is my code so far: 到目前为止,这是我的代码:

MainActivity.java: 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: 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: 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: 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: ContextGetter.java:

public class ContextGetter extends Application {

    private static Context context;

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

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

The exception: 例外:

java.lang.RuntimeException: Unable to start activity ComponentInfo{notes/notes.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference java.lang.RuntimeException:无法启动活动ComponentInfo {notes / notes.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)'在空对象引用上

Still can't seem to wrap my head around the context stuff. 似乎仍然无法将我的脑袋缠在上下文中。 Any help is appreciated! 任何帮助表示赞赏!

Also, what's a good way to implement loadAllNotes()? 另外,什么是实现loadAllNotes()的好方法?

Cheers 干杯

您应该添加name=".ContextGetter"以在<application>标记中显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM