android - java.lang.NullPointerException with context.getSharedPreferences(STORAGE_NAME, 0) -
i have created class working sharedpreferences. in activity, trying add item , following error...
06-05 17:01:53.950: e/androidruntime(3488): fatal exception: main 06-05 17:01:53.950: e/androidruntime(3488): java.lang.nullpointerexception 06-05 17:01:53.950: e/androidruntime(3488): @ com.xcxcxc.helpers.prefs.init(prefs.java:16) 06-05 17:01:53.950: e/androidruntime(3488): @ com.xcxcxc.helpers.prefs.addstringproperty(prefs.java:30) 06-05 17:01:53.950: e/androidruntime(3488): @ com.xcxcxc.usersadapter.splashactivity$1.callback(splashactivity.java:78) 06-05 17:01:53.950: e/androidruntime(3488): @ com.xcxcxc.usersadapter.splashactivity$1.callback(splashactivity.java:1) this code handling sharedpreferences...
import android.content.context; import android.content.sharedpreferences; public class prefs { public static final string storage_name = "applicationprefs"; private static sharedpreferences settings = null; private static sharedpreferences.editor editor = null; private static context context = null; public static void init(context cntxt) { context = cntxt; } private static void init() { settings = context.getsharedpreferences(storage_name, 0); editor = settings.edit(); } /* string values */ public static void addstringproperty(string name, string value) { if (settings == null || editor == null) { init(); } editor.putstring(name, value); editor.commit(); } public static string getstringproperty(string name) { if (settings == null || editor == null) { init(); } return settings.getstring(name, null); } } this code calling in activity...
prefs.addstringproperty("client_id", "1jdkv9sdfj8sf63rjs");
looks context null. set context before addstringproperty(..)
prefs.init(getapplicationcontext()); prefs.addstringproperty("client_id", "1jdkv9sdfj8sf63rjs");
Comments
Post a Comment