android - Store value using system preferences -


i trying store variables using system preferences.i tried tutorials, application keeps on crashing.can me out.i tried according this.also code :

    sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(this);     editor editor = sharedpreferences.edit(); 

this logcat output:

06-03 15:17:08.600: d/bstcommandprocessor-application(908): application crash has been observed.  06-03 15:17:08.600: i/process(10216): sending signal. pid: 10216 sig: 9 06-03 15:17:08.600: d/androidruntime(10216): procname cmdline: com.example.flash4 06-03 15:17:08.600: e/androidruntime(10216): in writecrashedappname, pkgname :com.example.flash4 06-03 15:17:08.600: d/androidruntime(10216): file written content: com.example.flash4 stringbuffer : ;com.example.flash4 06-03 15:17:08.600: e/androidruntime(10216): fatal exception: main 06-03 15:17:08.600: e/androidruntime(10216): java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.flash4/com.example.flash4.mainactivity}: java.lang.nullpointerexception 06-03 15:17:08.600: e/androidruntime(10216):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1880) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.app.activitythread.handlelaunchactivity(activitythread.java:1981) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.app.activitythread.access$600(activitythread.java:123) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.app.activitythread$h.handlemessage(activitythread.java:1147) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.os.handler.dispatchmessage(handler.java:99) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.os.looper.loop(looper.java:137) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.app.activitythread.main(activitythread.java:4424) 06-03 15:17:08.600: e/androidruntime(10216):    @ java.lang.reflect.method.invokenative(native method) 06-03 15:17:08.600: e/androidruntime(10216):    @ java.lang.reflect.method.invoke(method.java:511) 06-03 15:17:08.600: e/androidruntime(10216):    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:825) 06-03 15:17:08.600: e/androidruntime(10216):    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:592) 06-03 15:17:08.600: e/androidruntime(10216):    @ dalvik.system.nativestart.main(native method) 06-03 15:17:08.600: e/androidruntime(10216): caused by: java.lang.nullpointerexception 06-03 15:17:08.600: e/androidruntime(10216):    @ android.content.contextwrapper.getpackagename(contextwrapper.java:127) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.preference.preferencemanager.getdefaultsharedpreferencesname(preferencemanager.java:371) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.preference.preferencemanager.getdefaultsharedpreferences(preferencemanager.java:366) 06-03 15:17:08.600: e/androidruntime(10216):    @ com.example.flash4.mainactivity.<init>(mainactivity.java:71) 06-03 15:17:08.600: e/androidruntime(10216):    @ java.lang.class.newinstanceimpl(native method) 06-03 15:17:08.600: e/androidruntime(10216):    @ java.lang.class.newinstance(class.java:1319) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.app.instrumentation.newactivity(instrumentation.java:1025) 06-03 15:17:08.600: e/androidruntime(10216):    @ android.app.activitythread.performlaunchactivity(activitythread.java:1871) 06-03 15:17:08.600: e/androidruntime(10216):    ... 11 more 06-03 15:17:08.690: i/activitymanager(610): process com.example.flash4 (pid 10216) has died. 06-03 15:17:08.690: w/activitymanager(610): force removing activityrecord{b335c7d8 com.example.flash4/.mainactivity}: app died, no saved state 

try following code:

sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(getbasecontext()); 

instead of:

sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(this); 

edit

if not doing this, place above code in oncreate() method. if want preferences available elsewhere, do

sharedpreferences sharedpreferences; 

so gets declared global, in oncreate()

sharedpreferences = preferencemanager.getdefaultsharedpreferences(getbasecontext()); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -