nullpointerexception - Android Crash after using SharedPreferences -
i developing android application, keep me logged in functionality using sharedpreferences application started crash.
this line add preferences;
sharedpreferences pref = getapplicationcontext().getsharedpreferences("rs_remember" , getapplicationcontext().mode_private);
i pretty sure line causing error because works fine if remove it.
error message says:
java.lang.runtime: unable create application com.x.x.x : java.lang.nullpointerexception.
any idea abot shuold do?
edit
public activity activity; sharedpreferences pref; private rscurrentusermanager() { super(); pref = this.activity.getsharedpreferences("rallyspark_remember",0); } +05-09 18:02:56.221: e/androidruntime(31760): fatal exception: main +05-09 18:02:56.221: e/androidruntime(31760): java.lang.runtimeexception: unable create application com.x.x.general.rsapplication: java.lang.nullpointerexception +05-09 18:02:56.221: e/androidruntime(31760): @ com.x.x.general.rscurrentusermanager.<init>(rscurrentusermanager.java:93) +05-09 18:02:56.221: e/androidruntime(31760): @ com.x.x.general.rscurrentusermanager.getinstance(rscurrentusermanager.java:84)
i not using shared preferences in other pleace in order find problem removed execpt defination
if inside of activity
method shouldn't need use context
. if aren't or inside of inner class or listener replcae
sharedpreferences pref = getapplicationcontext().getsharedpreferences("rs_remember" , getapplicationcontext().mode_private);
with
sharedpreferences pref = activityname.this.getsharedpreferences("rs_remember" , getapplicationcontext().mode_private);
most of time want use activity context
instead of application context
. can find many posts information 2 on so. i'm pretty sure problem if isn't please post full logcat , show line in code.
this answer has explanation of two
Comments
Post a Comment