Android ListView filter fail with NPE -
in app have listview, adapter it, , filter adapter. filter:
private filter namefilter = new filter() { @override protected void publishresults(charsequence constraint, filterresults results) { list<contact> resultcontacts = (list<contact>) results.values; filteredcontacts = resultcontacts; notifydatasetchanged(); } @override protected filterresults performfiltering(charsequence constraint) { list<contact> filteredcontacts = new arraylist<contact>(); for(contact c : rawcontacts){ if(!c.getname().tolowercase().contains(constraint.tostring().tolowercase())){ continue; } filteredcontacts.add(c); } filterresults filterresults = new filterresults(); filterresults.values = filteredcontacts; return filterresults; } };
do understand correctly publishresults invoked after performfiltering done job? job veeeery hard , long? have situation when publishresults runs , of course field "filteredcontacts" of adapter sets null.
i tried make easier job of performfiltering (by removing cycle , write "filteredcontacts.add(new contact())") , works fine.
and code worked fine few days... confused. can explain going on?) in advance!
ok guys! got it! performfiltering(...) runs in worker thread , when
if(!c.getname().tolowercase().contains(constraint.tostring().tolowercase())){ continue; }
fails nullpointerexception on c.getname().tolowercase() (because getname() returns null), it's thread fails too, seems sdk somewhere cathes exception , thread die.
also, publishresults can invoked @ time ui thread null checking required:
@override protected void publishresults(charsequence constraint, filterresults results) { if(results.values != null) { list<contact> resultcontacts = (list<contact>) results.values; filteredcontacts = resultcontacts; notifydatasetchanged(); } }
Comments
Post a Comment