android - How to getSharedPreferences in Application? -
i tried call getsharedpreferences
in application...
public class app extends application{ public app() { sharedpreferences prefs = getsharedpreferences(shared_preferences_name, context.mode_private); } }
...but got nullpointerexception
:
java.lang.nullpointerexception: attempt invoke virtual method 'android.content.sharedpreferences android.content.context.getsharedpreferences(java.lang.string, int)' on null object reference
i tried , got same exception:
context con = getapplicationcontext();
how can call getsharedpreferences
?
override oncreate()
, in app,
@override public void oncreate() { super.oncreate();
and there. don't forget declare application
subclass in androidmanifest
well. e.g.
<application android:name="app"
Comments
Post a Comment