java - Storing and getting values from sharedpreferences in android -


i want store values in sharedpreferences , getting values sharedpreferences display values in listview. next want remove values list when click on longpress on specific value .after removing values status updated in sharedpreferences. done 1 problem when close application , open again have display sharedpreferences vales in listview. display nullpointerexeption,

actually requirement 1)storing values in sharedpreferences , display values in listview every click .allowed 5 values.and list values available in onresume() method also. 2) when long press on specific value of listview have remove , and updated values stored in sharedpreferences also.

my code

public class listviewdemo1 extends activity {     /**      * called when activity first created.      */     button btn;     static int count;     private listview list;     public static arraylist<string> values = new arraylist<string>();             arraylist countlist = new arraylist();     private arrayadapter adapter;     sharedpreferences shared;     editor editor;     private static arraylist<string> sharedlist;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.main);          list = (listview) findviewbyid(r.id.list);         btn = (button) findviewbyid(r.id.btn);         shared = this.getsharedpreferences("myprefernces", context.mode_world_writeable);         editor = shared.edit();         btn.setonclicklistener(new onclicklistener() {              public void onclick(view v) {                 // todo auto-generated method stub                 sharedlist = new arraylist();                 count++;                 if (count == 1) {                     values.add("one");                     countlist.add(count);                 }                 if (count == 2) {                     values.add("two");                     countlist.add(count);                 }                 if (count == 3) {                     values.add("three");                     countlist.add(count);                 }                 if (count == 4) {                     values.add("four");                     countlist.add(count);                  }                 if (count == 5) {                     values.add("five");                     countlist.add(count);                 }                 if (count > 5) {                     --count;                     toast.maketext(getapplicationcontext(), "" + count, 100).show();                 }                 //put values sharedpreferences                 editor.putint("size", values.size());                 (int = 0; < values.size(); i++) {                     editor.putstring("addr" + i, values.get(i));                 }                 editor.commit();                 // getting values sharedpreference                 int size = shared.getint("size", 0);                 (int k = 0; k < size; k++) {                     sharedlist.add(shared.getstring("addr" + k, ""));                 }                 adapter = new                         arrayadapter(listviewdemo1.this, android.r.layout.simple_list_item_1, sharedlist);                 list.setadapter(adapter);             }         });         list.setonitemlongclicklistener(new onitemlongclicklistener() {              public boolean onitemlongclick(adapterview<?> arg0, view arg1,                                            int arg2, long arg3) {                 // todo auto-generated method stub                 --count;                 values.remove(arg2);                 sharedlist.remove(arg2);                 editor.clear();                 editor.commit();                 editor.putint("size", sharedlist.size());                 (int = 0; < sharedlist.size(); i++) {                     editor.putstring("addr" + i, sharedlist.get(i));                 }                 editor.commit();                 adapter.notifydatasetchanged();                   return true;             }         });      }      @override     protected void onresume() {         // todo auto-generated method stub         super.onresume();         if (sharedlist.size() > 0) {             adapter = new                     arrayadapter(listviewdemo1.this, android.r.layout.simple_list_item_1, sharedlist);             list.setadapter(adapter);         }     } } 

logcat

04-21 09:56:02.142: e/androidruntime(1160): fatal exception: main 04-21 09:56:02.142: e/androidruntime(1160): java.lang.runtimeexception: unable resume activity {com.views/com.views.listviewdemo1}: java.lang.nullpointerexception 04-21 09:56:02.142: e/androidruntime(1160):     @ android.app.activitythread.access$600(activitythread.java:141) 04-21 09:56:02.142: e/androidruntime(1160):     @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 04-21 09:56:02.142: e/androidruntime(1160):     @ android.os.handler.dispatchmessage(handler.java:99) 04-21 09:56:02.142: e/androidruntime(1160):     @ android.os.looper.loop(looper.java:137) 04-21 09:56:02.142: e/androidruntime(1160):     @ android.app.activitythread.main(activitythread.java:5041) 04-21 09:56:02.142: e/androidruntime(1160):     @ java.lang.reflect.method.invokenative(native method) 04-21 09:56:02.142: e/androidruntime(1160):     @ java.lang.reflect.method.invoke(method.java:511) 04-21 09:56:02.142: e/androidruntime(1160):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 04-21 09:56:02.142: e/androidruntime(1160):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 04-21 09:56:02.142: e/androidruntime(1160):     @ dalvik.system.nativestart.main(native method) 04-21 09:56:02.142: e/androidruntime(1160): caused by: java.lang.nullpointerexception 04-21 09:56:02.142: e/androidruntime(1160):     @ com.views.listviewdemo1.onresume(listviewdemo1.java:114) 04-21 09:56:02.142: e/androidruntime(1160):     @ android.app.instrumentation.callactivityonresume(instrumentation.java:1185) 

you instantiate sharedlist in onclick() method of button. method not called when activity resumed , sharedlist null.

you need check if it's not null:

@override protected void onresume() {     // todo auto-generated method stub     super.onresume();     if(sharedlist != null && sharedlist.size()>0){         adapter=new arrayadapter(listviewdemo1.this,android.r.layout.simple_list_item_1,sharedlist);         list.setadapter(adapter);     } }  

Comments

Popular posts from this blog

jQuery Mobile app not scrolling in Firefox -

c++ - How to add Crypto++ library to Qt project -

how to receive file in java(servlet/jsp) -