NullpointerException when Launching activity

百般思念 提交于 2019-12-02 09:29:55

best practice for java development is to have the literal string do the .equals call. so instead of:

var.equals("string")

you do:

"string".equals(var)

This guarantees you will NEVER have a null pointer exception when doing string comparison.

Also, it looks like you are storing numeric values as strings. Any particular reason you aren't storing them as ints?

Its likely here

ln=settings.getString("LASTREADln", null);

this should be

ln=settings.getString("LASTREADln", "");

since null is set to be your default value if that key does not exist or contain anything, so if it doesn't contain anything you should set it to "", and for your string comparisons you should look for !ln.contentsEquals("") instead of checking it for null

the same goes for all of the strings you get from a preferences file. set the default value to "" instead of null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!