Android Filter a listview Attempt to invoke interface method 'int java.util.List.size()' on a null object reference -
my aim filter listview research, "whatsapp" when looking friend name. have error when try write inside search box appears if search icon pressed.
this error:
process: org.testing.an_app, pid: 15386 java.lang.nullpointerexception: attempt invoke interface method 'int java.util.list.size()' on null object reference @ android.widget.simpleadapter.getcount(simpleadapter.java:93) @ android.widget.adapterview.checkfocus(adapterview.java:717) @ android.widget.adapterview$adapterdatasetobserver.oninvalidated(adapterview.java:840) @ android.widget.abslistview$adapterdatasetobserver.oninvalidated(abslistview.java:6137) @ android.database.datasetobservable.notifyinvalidated(datasetobservable.java:50) @ android.widget.baseadapter.notifydatasetinvalidated(baseadapter.java:59) @ android.widget.simpleadapter$simplefilter.publishresults(simpleadapter.java:383) @ android.widget.filter$resultshandler.handlemessage(filter.java:282) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5254) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:898) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:693)
and code create listview custom items:
import android.app.actionbar; import android.app.activity; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.text.textutils; import android.util.log; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.widget.listadapter; import android.widget.listview; import android.widget.searchview; import android.widget.simpleadapter; import java.util.arraylist; import java.util.hashmap; import java.util.random; public class friends_list_screen extends activity { private searchview msearchview; private listview listview; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_friends_list); listview = (listview) findviewbyid(r.id.friends); create_list(); listview.settextfilterenabled(true); } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.friend_list_screen_menu, menu); msearchview = (searchview) menu.finditem(r.id.search).getactionview(); if (integer.valueof(android.os.build.version.sdk) > 7) { msearchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string s) { return false; } @override public boolean onquerytextchange(string s) { listview = (listview) findviewbyid(r.id.friends); simpleadapter adapter = (simpleadapter) listview.getadapter(); if (textutils.isempty(s)) { adapter.getfilter().filter(""); } else { adapter.getfilter().filter(s.tostring().tolowercase()); } return true; } }); } final menuitem searchmi = (menuitem) menu.finditem(r.id.search); msearchview.setonquerytextfocuschangelistener(new view.onfocuschangelistener() { @override public void onfocuschange(view view, boolean querytextfocused) { if (!querytextfocused) { searchmi.collapseactionview(); } } }); return true; } private void create_list(){ arraylist<friend_obj> friend_objlist=new arraylist<friend_obj>(); friend_obj [] people={ new friend_obj("x","b",r.drawable.profile_icon), new friend_obj("y", "q", r.drawable.profile_icon), new friend_obj("z","d",r.drawable.profile_icon), new friend_obj("w","x",r.drawable.profile_icon)}; random r=new random(); for(int i=0;i<100;i++){ friend_objlist.add(people[r.nextint(people.length)]); } arraylist<hashmap<string, object>> data=new arraylist<hashmap<string,object>>(); for(int i=0;i<friend_objlist.size();i++){ friend_obj p=friend_objlist.get(i); hashmap<string,object> friend_objmap=new hashmap<string, object>(); friend_objmap.put("image", p.getphotores()); friend_objmap.put("name", p.getname()); friend_objmap.put("surname", p.getsurname()); data.add(friend_objmap); } string[] from={"image","name","surname"}; int[] to={r.id.personimage,r.id.personname,r.id.personsurname}; simpleadapter adapter=new simpleadapter( getapplicationcontext(), data, r.layout.friends_list_item, from, to); listview.setadapter(adapter); } }
perhaps didn't set adapter listview?
btw - why name variable same name class type? (listview listview)
Comments
Post a Comment