android - java.lang.NullPointerException SharedPreferences getSharedPreferences -


so having weird problem, solvable else me ;-) anyway, having many activities , on every activity have problem resume properly, every activity one.

whenever start 1 activity, or put 1 activity sleep , return it, works fine. when put sleep hit home button , launch bunch of different apps , try return again getting java.lang.nullpointerexception @ getsharedpreferences.

how can happen? sharedpreferences deleted?

here getpreferences method error happens:

private static sharedpreferences getpreferences() {     context applicationcontext = app.getcontextofapplication();     return applicationcontext.getsharedpreferences(prefs_user, context.mode_private); } 

here logcat:

05-16 20:37:36.461: e/androidruntime(13767): fatal exception: main 05-16 20:37:36.461: e/androidruntime(13767): process: com.test, pid: 13767 05-16 20:37:36.461: e/androidruntime(13767): java.lang.runtimeexception: unable start activity componentinfo{com.test/com.test.messageactivity}: java.lang.nullpointerexception 05-16 20:37:36.461: e/androidruntime(13767):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2305) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:2363) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.app.activitythread.access$900(activitythread.java:161) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.app.activitythread$h.handlemessage(activitythread.java:1265) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.os.handler.dispatchmessage(handler.java:102) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.os.looper.loop(looper.java:157) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.app.activitythread.main(activitythread.java:5356) 05-16 20:37:36.461: e/androidruntime(13767):    @ java.lang.reflect.method.invokenative(native method) 05-16 20:37:36.461: e/androidruntime(13767):    @ java.lang.reflect.method.invoke(method.java:515) 05-16 20:37:36.461: e/androidruntime(13767):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) 05-16 20:37:36.461: e/androidruntime(13767):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) 05-16 20:37:36.461: e/androidruntime(13767):    @ dalvik.system.nativestart.main(native method) 05-16 20:37:36.461: e/androidruntime(13767): caused by: java.lang.nullpointerexception 05-16 20:37:36.461: e/androidruntime(13767):    @ com.test.localstorage.getpreferences(localstorage.java:30) 05-16 20:37:36.461: e/androidruntime(13767):    @ com.test.localstorage.getusername(localstorage.java:83) 05-16 20:37:36.461: e/androidruntime(13767):    @ com.test.webapi.getchat(webapi.java:711) 05-16 20:37:36.461: e/androidruntime(13767):    @ com.test.messageactivity.oncreate(messageactivity.java:100) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.app.activity.performcreate(activity.java:5426) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1105) 05-16 20:37:36.461: e/androidruntime(13767):    @ android.app.activitythread.performlaunchactivity(activitythread.java:2269) 05-16 20:37:36.461: e/androidruntime(13767):    ... 11 more 

thanks in advance!

as @niek_haarman indicated, cannot rely on static reference context instance, if application instance, since android system can destroy , reconstruct application , activity instances. so, if app.getcontextofapplication() returning static variable value, may going wrong.

is there reason getpreferences() must static? if can make non-static method of activity, should have no problem using activity context:

private sharedpreferences getpreferences() {     return getsharedpreferences(prefs_user, context.mode_private); } 

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -