java - Before loading listview from json url, giving error at onQueryTextChange(),when applying getFilter() -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i new in android development ,i have implemented searchfilter
searchview
. applied filter listview
, giving me correct result. problem that, when listview
still loading ...at time if click on search edit text,my app gets crash....
my code:
public class cityadapter extends baseadapter implements filterable { valuefilter valuefilter; private arraylist<city> citylist; private arraylist<city> mstringfilterlist; private context context; private layoutinflater layoutinflater; public cityadapter(context context, arraylist<city> citylist) { this.context = context; this.citylist = citylist; mstringfilterlist = citylist; layoutinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } @override public int getcount() { return citylist.size(); } @override public object getitem(int position) { return citylist.get(position); } @override public long getitemid(int position) { return position; } @override public filter getfilter() { if (valuefilter == null) { valuefilter = new valuefilter(); } return valuefilter; } public class holder { robototextview cityname_txt; textview cityid_txt; } @override public view getview(int position, view convertview, viewgroup parent) { final holder holder; if (convertview == null) { holder = new holder(); convertview = layoutinflater.inflate(r.layout.city_list_item, null); holder.cityname_txt = (robototextview) convertview.findviewbyid(r.id.citytxt); holder.cityid_txt = (textview) convertview.findviewbyid(r.id.idtxt); convertview.settag(holder); } else { holder = (holder) convertview.gettag(); } holder.cityname_txt.settext(citylist.get(position).getcity_name()); holder.cityid_txt.settext(citylist.get(position).getcity_id()); return convertview; } private class valuefilter extends filter { @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); if (constraint != null && constraint.length() > 0) { arraylist<city> filterlist = new arraylist<city>(); (int = 0; < mstringfilterlist.size(); i++) { if ((mstringfilterlist.get(i).getcity_name().touppercase()) .contains(constraint.tostring().touppercase())) { city city = new city(mstringfilterlist.get(i) .getcity_id(), mstringfilterlist.get(i) .getcity_name()); filterlist.add(city); } } results.count = filterlist.size(); results.values = filterlist; } else { results.count = mstringfilterlist.size(); results.values = mstringfilterlist; } return results; } @override protected void publishresults(charsequence constraint, filterresults results) { citylist = (arraylist<city>) results.values; notifydatasetchanged(); } } }
my activity code
search_view.setonquerytextlistener(this); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string itemid = (string) lv.getitematposition(position).tostring(); toast.maketext(context,"position="+itemid,toast.length_long).show(); intent i_details = new intent(dharmshalacities.this, dharmshaladetailsactivity.class); i_details.putextra("itemid", itemid); startactivity(i_details); } }); } @override public boolean onquerytextsubmit(string query) { return false; } @override public boolean onquerytextchange(string newtext) { cityadapter.getfilter().filter(newtext); return false; } //and giving error line of code //cityadapter.getfilter().filter(newtext);
logcat error
06-16 07:22:19.326 23888-23888/com.newsoftsolutions.android.padamashligotram e/androidruntime: fatal exception: main process: com.newsoftsolutions.android.padamashligotram, pid: 23888 java.lang.nullpointerexception: attempt invoke virtual method 'android.widget.filter com.newsoftsolutions.android.padamashligotram.adapter.cityadapter.getfilter()' on null object reference @ com.newsoftsolutions.android.padamashligotram.dharmshalacities.onquerytextchange(dharmshalacities.java:76) @ android.widget.searchview.ontextchanged(searchview.java:1257) @ android.widget.searchview.access$2000(searchview.java:95) @ android.widget.searchview$11.ontextchanged(searchview.java:1737) @ android.widget.textview.sendontextchanged(textview.java:7749) @ android.widget.textview.handletextchanged(textview.java:7809) @ android.widget.textview$changewatcher.ontextchanged(textview.java:9727) @ android.text.spannablestringbuilder.sendtextchanged(spannablestringbuilder.java:964) @ android.text.spannablestringbuilder.replace(spannablestringbuilder.java:515) @ android.text.spannablestringbuilder.replace(spannablestringbuilder.java:454) @ android.text.spannablestringbuilder.replace(spannablestringbuilder.java:33) @ android.view.inputmethod.baseinputconnection.replacetext(baseinputconnection.java:685) @ android.view.inputmethod.baseinputconnection.committext(baseinputconnection.java:197) @ com.android.internal.widget.editableinputconnection.committext(editableinputconnection.java:184) @ com.android.internal.view.iinputconnectionwrapper.executemessage(iinputconnectionwrapper.java:286) @ com.android.internal.view.iinputconnectionwrapper$myhandler.handlemessage(iinputconnectionwrapper.java:78) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5289) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:899) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:694) 06-16 07:22:20.643 23888-24385/com.newsoftsolutions.android.padamashligotram e/pass 1: connection success
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (youradapter != null) { string itemid = (string) lv.getitematposition(position).tostring(); toast.maketext(context, "position=" + itemid, toast.length_long).show(); intent i_details = new intent(dharmshalacities.this, dharmshaladetailsactivity.class); i_details.putextra("itemid", itemid); startactivity(i_details); } }
change here too
@override public boolean onquerytextchange(string newtext) { if (cityadapter != null) { cityadapter.getfilter().filter(newtext); } return false; }
Comments
Post a Comment